Question

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.

Was it helpful?

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).

OTHER TIPS

As far as I understand you have to send an Dictionary of keys and objects to the watch. In Objective C it looks like this:

NSDictionary *update = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

Keys are all integer and objects I would think can be of Boolean. In my case only String what works well.

In a loop I perform something like this on the watch:

t=dict_find(received,count);
strcpy(strs[count], dict_find(received,count)->value->cstring);

But I have to tell you that I am still a rookie.

Thanks to sarfata's accepted answer above, I found that I had not added this new item to the Tuple that the Pebble was expecting.

Once I added that, my switch statement started working and I just had to get the memory compare to work. Here is the working code for my memory compare in case it helps anyone.

case GPS_HAS_FIX_KEY:
if (memcmp(s_data.haveGPS, new_tuple->value->data, 1) != 0){
  memcpy(s_data.haveGPS,new_tuple->value->data, 1);
  vibes_short_pulse();
}
break;

It really was a simple as expecting one byte (not 8 bytes - I thought it was doing a bit compare) and negating the logic for the case where the new value is NOT like the old one.

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