Search code examples
androidcpebble-watchpebble-sdk

Sending boolean to Pebble with PebbleKit


I've never really done much C and am a bit stumped on the best way to send a boolean from an Android app to the Pebble Watch.

I have strings working fine, but there doesn't seem to be an addBoolean method on PebbleDictionary. As a work around I am trying to use addUint8 to send a 1 or 0, but am having trouble handling the message on the Pebble.

Here is my Android code:

    PebbleDictionary data = new PebbleDictionary();
    if (isGPSFix()){
        data.addUint8(GPS_HAS_FIX_KEY, Byte.valueOf("1"));
    } else {
        data.addUint8(GPS_HAS_FIX_KEY, Byte.valueOf("0"));
    }
    PebbleKit.sendDataToPebble(app.getContext(), UUID, data);

And in my Pebble I have a data struct:

static struct MyData {
  uint8_t haveGPS[1];
  .... // other stuff ommitted
  AppSync sync;
  uint8_t sync_buffer[256];
} s_data;

And then I am trying to compare it like this in my sync_tuple_changed callback.

static void sync_tuple_changed_callback(const uint32_t key, const Tuple* new_tuple, const Tuple* old_tuple, void* context) {
(void) old_tuple;

   switch (key) {
     case GPS_HAS_FIX_KEY:
       if (memcmp(s_data.haveGPS, new_tuple->value->data, 8) == 0){
         memcpy(s_data.haveGPS,new_tuple->value->data, new_tuple->length);
         vibes_short_pulse();
       }
     break;
     default:
       return;
   }
 }

The watch doesn't crash, it just never vibrates when the phone drops or acquires GPS.


Solution

  • Things look good on the Android side. I think this is more of an AppSync problem.

    Here are a few things to check in the watch application:

    • Make sure you create a list of tuples with initial values on the watch. This list needs to to contain your key GPS_HAS_FIX_KEY;
    Tuplet initial_values[] = {
      TupletInteger(GPS_HAS_FIX_KEY, (uint8_t) 0),
      /* Other tuplets that you will synchronize */
    }; 
    
    • Make sure you pass those tuplets to the app_sync_init() function:
    app_sync_init(&sync, sync_buffer, sizeof(sync_buffer),
                  initial_values, ARRAY_LENGTH(initial_values),
                  sync_tuple_changed_callback, sync_error_callback, NULL);
    

    Those two steps are required for app_sync to work (cf AppSync reference documentation).