Question

I am using sdk2 for pebble, with the js appmessage features:

I am trying to send consecutive messages to pebble js on the phone, for each one of my menu items. A variable movie_count = 5 exists, and I use this for looping, It gets logged out as 5 as the code shows below, so it should be getting to all 5 , logging errors at least, but it just doesn't log anything after the first time:

static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
 int i;
 APP_LOG(APP_LOG_LEVEL_DEBUG, "movie_count int %u", movie_count);
 for(i = 0;i<movie_count;i++){
    Tuplet build_menu_tuple =  TupletInteger(BUILD_MENU_KEY, 1); // just a flag for the  appmessage js code
    Tuplet menu_id_tuple =  TupletInteger(MENU_ID_KEY, i);
    DictionaryIterator *iter;
    app_message_outbox_begin(&iter);
    if (iter == NULL) {
       return;
    }
    dict_write_tuplet(iter, &build_menu_tuple);
    dict_write_tuplet(iter, &menu_id_tuple);
    dict_write_end(iter);
    app_message_outbox_send();
}
}

in JS appmessage

this code in the js is executed, although only once, I have logged the output in my in_receiver() callback in my watchapp, and my first item gets logged, but the logger just quits after that... Is this because the watchapp cannnot send blutooth messages in a loop like this? Is there a way to make sure the message is sent, or pause the execution so it sends at a slower pace? (the movies_json exists above the code below, i left it out for brevity, but it is there, a json object with an inner array of movies)

if(e.payload.build_menu){

            var menu_id = e.payload.menu_id;

            console.log("menu_id" + menu_id);

            Pebble.sendAppMessage({"title":movies_json.movies[menu_id].title,

                                   "stars":movies_json.movies[menu_id].stars,

                                   "menu_id":menu_id

            });

            console.log("movie title:" + movies_json.movies[i].title);

        }

in_recived_handler callback code to handle messages from the js

this code is in the callback that takes in messsages from the phones js... it only gets to the first item, logs just the first item's menu_id and title, and then logging just stops.

if(menu_id_tuple){
  int menu_id;
  menu_id = menu_id_tuple->value->int32;
  char movie_title[30];
  strncpy(movie_title, movie_title_tuple->value->cstring, 30);
  APP_LOG(APP_LOG_LEVEL_DEBUG, "In received handler movie_title: %s" , movie_title);
  APP_LOG(APP_LOG_LEVEL_DEBUG, "In received handler menu_id: %u" , menu_id);

}

Was it helpful?

Solution

You need to wait until the first message is sent to send the next one.

The proper way to do this is to register a callback for the outbox_sent event and to queue the next message in this event.

Explanation

There is only one bluetooth buffer on pebble and it can only hold one message at a time. If you send messages in a for loop, this buffer gets filled with the first message and all the other messages are rejected.

You would see the error messages if you checked the return value of app_message_outbox_send(). You should also implement a AppMessageOutboxFailed handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top