我有一个Pebble智能手表表盘,可以从应用程序的配置页面(html页面)中的textarea获取文本,并将该值放入表盘上的文本层。
不幸的是,有两件事情导致它无法正常工作(希望它们都能通过一种解决方案解决):
1)返回车厢(例如\ n \ n不在文本图层上工作,而不是移动到新行,它只显示'\ n'字符 2)不成对的'(撇号)和“(引号)不更新页面(即只是不起作用)
我在使用watchface和config之间的通信方面并不令人惊讶,但除了这个问题之外,其他一切似乎都很好。下面是我从文本区域到文本层获取文本的路径。
相关脚本(在config.html中)
[].forEach.call(document.querySelectorAll("#save"), function(e1) {
e1.addEventListener("click", function() {
console.log(saveOptions());
var return_to = getQueryParam('return_to', 'pebblejs://close#');
document.location = return_to + encodeURIComponent(JSON.stringify(saveOptions()));
});
});
相关的javascript(script.js)
Pebble.addEventListener('webviewclosed', function(e) {
var options = JSON.parse(decodeURIComponent(e.response));
message = options.message;
var dict = {
'MESSAGE_DATA' : message
};
//Send a string to Pebble
Pebble.sendAppMessage(dict, function(e) {
console.log("Send successful.");
}, function(e) {
console.log("Send failed!");
});
}
相关main.c:
static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
APP_LOG(APP_LOG_LEVEL_INFO, "Message received!");
// Get the first pair
Tuple *t = dict_read_first(iterator);
// Process all pairs present
while(t != NULL) {
// Process this pair's key
switch (t->key) {
case MESSAGE_DATA:
snprintf(message_buffer, sizeof(message_buffer), "%s", t->value->cstring);
APP_LOG(APP_LOG_LEVEL_INFO, "MESSAGE_DATA received with value %d", (int)t->value->int32);
break;
default:
APP_LOG(APP_LOG_LEVEL_ERROR, "Key %d not recognized!", (int)t->key);
break;
}
// Get next pair, if any
t = dict_read_next(iterator);
}
}
编辑:添加message.replace(/[\n\r]/g, ' ');
无法解决问题1:/
答案 0 :(得分:0)
我认为这在JS和C之间的转换中会丢失,因为C 将正确显示返回的车厢。您应该考虑使用特殊字符来替换所有可能不会出现在您要发送的实际消息中的#或$等回程车厢。然后,迭代C中的字符数组,并用\ n。
替换该特殊字符的所有实例