Hello!
In the post from 2 days ago, I put a small example on how to use the JS files in the world of development of Pebble. This files are executed in the smartphone that is associated with the pebble and today, I’m going to extend the previous example to show how information can be sent from the JS to the Pebble.
On the above example, I modified the JS with the following changes
-According to the option that is received from the Pebble, a specific value is stored in the variable selectedButton (line 9)
-At the end of the evaluation, be sends this value to the Pebble using Pebble.sendAppMessage (line 27). It defines message dictionary item is used and we also have the possibility of using callbacks to process the errors and the Ok of the function.
In the file C have the following changes
-In line 95 is subscription to input in the function in_received_handler messages
-Each one of the elements which are received in the dictionary for the exchange of messages processed this function (line 11)
-Finally in line 28, it evaluates the message and displays the information that is received from the Pebble
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <pebble.h> | |
| static Window *window; | |
| static TextLayer *text_layer; | |
| enum { | |
| KEY_EXCHANGEDATA = 0, | |
| }; | |
| // END Interaction with JS | |
| static void in_received_handler(DictionaryIterator *iter, void *context) | |
| { | |
| (void) context; | |
| Tuple *t = dict_read_first(iter); | |
| while (t != NULL) | |
| { | |
| process_tuple(t); | |
| t = dict_read_next(iter); | |
| } | |
| } | |
| void process_tuple(Tuple *t) | |
| { | |
| int key = t->key; | |
| int value = t->value->int32; | |
| char string_value[32]; | |
| strcpy(string_value, t->value->cstring); | |
| switch (key) { | |
| case KEY_EXCHANGEDATA: | |
| snprintf(sample_buffer, sizeof("XX button: \u00B0C"), "%d button: \u00B0C", value); | |
| text_layer_set_text(text_layer, (char*) &sample_buffer); | |
| break; | |
| } | |
| } | |
| void send_int(uint8_t key, uint8_t cmd) | |
| { | |
| DictionaryIterator *iter; | |
| app_message_outbox_begin(&iter); | |
| Tuplet value = TupletInteger(key, cmd); | |
| dict_write_tuplet(iter, &value); | |
| app_message_outbox_send(); | |
| } | |
| // END Interaction with JS | |
| void select_click_handler(ClickRecognizerRef recognizer, void *context) { | |
| APP_LOG(APP_LOG_LEVEL_DEBUG, "click SELECT"); | |
| text_layer_set_text(text_layer, "Select"); | |
| send_int(0, 1); | |
| } | |
| void up_click_handler(ClickRecognizerRef recognizer, void *context) { | |
| APP_LOG(APP_LOG_LEVEL_DEBUG, "click UP"); | |
| text_layer_set_text(text_layer, "Up"); | |
| send_int(0, 2); | |
| } | |
| void down_click_handler(ClickRecognizerRef recognizer, void *context) { | |
| APP_LOG(APP_LOG_LEVEL_DEBUG, "click DOWN"); | |
| text_layer_set_text(text_layer, "Down"); | |
| send_int(0, 3); | |
| } | |
| void click_config_provider(void *context) { | |
| window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler); | |
| window_single_click_subscribe(BUTTON_ID_UP, up_click_handler); | |
| window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler); | |
| } | |
| void window_load(Window *window) { | |
| Layer *window_layer = window_get_root_layer(window); | |
| GRect bounds = layer_get_bounds(window_layer); | |
| text_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, 20 } }); | |
| text_layer_set_text(text_layer, "Press a button"); | |
| text_layer_set_text_alignment(text_layer, GTextAlignmentCenter); | |
| layer_add_child(window_layer, text_layer_get_layer(text_layer)); | |
| } | |
| void window_unload(Window *window) { | |
| text_layer_destroy(text_layer); | |
| } | |
| void init(void) { | |
| window = window_create(); | |
| window_set_click_config_provider(window, click_config_provider); | |
| window_set_window_handlers(window, (WindowHandlers) { | |
| .load = window_load, | |
| .unload = window_unload, | |
| }); | |
| const bool animated = true; | |
| window_stack_push(window, animated); | |
| //Register AppMessage events | |
| app_message_register_inbox_received(in_received_handler); | |
| app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); //Largest possible input and output buffer sizes | |
| } | |
| void deinit(void) { | |
| window_destroy(window); | |
| } | |
| int main(void) { | |
| init(); | |
| app_event_loop(); | |
| deinit(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Pebble.addEventListener('ready', | |
| function (e) { | |
| console.log("JS4 Pebble.addEventListener ready "); | |
| }); | |
| Pebble.addEventListener("appmessage", | |
| function (e) { | |
| try { | |
| var selectedButton = "undefined"; | |
| console.log(JSON.stringify(e.payload)); | |
| if (e.payload["exchangeData"] == 1) | |
| { | |
| console.log("Button SELECT"); | |
| selectedButton = "SELECT"; | |
| } | |
| if (e.payload["exchangeData"] == 2) | |
| { | |
| console.log("Button UP"); | |
| selectedButton = "UP"; | |
| } | |
| if (e.payload["exchangeData"] == 3) | |
| { | |
| console.log("Button DOWN"); | |
| selectedButton = "DOWN"; | |
| } | |
| Pebble.sendAppMessage({ exchangeData: selectedButton }, | |
| function (e) { | |
| console.log("Successfully delivered message with transactionId=" + e.data.transactionId); | |
| }, | |
| function (e) { | |
| console.log("Unable to deliver message with transactionId=" + e.data.transactionId + " Error is: " + e.error.message); | |
| } | |
| ); | |
| } catch (exc) { | |
| console.log("exception" + exc.message); | |
| } | |
| } | |
| ); |
Reference: https://elbruno.com/2014/10/01/pebble-how-to-use-a-js-file-from-the-pebble/
Greetings @ Home
The Bruno
Leave a comment