I have a Pebble smartwatch watchface which gets text from a textarea in the app's config page (html page), and puts that value into a text layer on the watchface.
Unfortunately there are two things that cause this to not work as intended (hopefully they'll both be solved with one solution):
1) Return carriages (eg. \n don't work on the text layer, instead of moving to a new line it just displays the '\n' character 2) Unpaired ' (apostrophes) and " (quotation marks) don't update the page (i.e just don't work)
I'm not amazing at working with the communication between my watchface and config but everything else seems to work fine apart from this one issue. So below is the path I took to getting the text from the text area to the text layer.
Relevant script (in 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()));
});
});
Relevant 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!");
});
}
Relevant 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);
}
}
Edit: Adding message.replace(/[\n\r]/g, ' ');
didn't work to solve problem 1 :/
I think this is lost in translation between JS and C, since C will display return carriages correctly. You should consider using a special character to replace all return carriages like # or $ that likely won't appear in the actual message you want to send. Then, iterate through your character array in C and replace all instances of that special character with \n.