pebble development - using appmessage for two way communication

43
PebbleKit Introducing 2-way communication

Upload: pebble-technology

Post on 28-Jan-2015

108 views

Category:

Technology


1 download

DESCRIPTION

This presentation introduces the AppMessage framework which is part of Pebble SDK. It allows developer to exchange messages between a watch and a phone with just a few APIs. Pebble SDK is available from http://developer.getpebble.com

TRANSCRIPT

Page 1: Pebble Development - Using AppMessage for two way communication

PebbleKitIntroducing 2-way communication

Page 2: Pebble Development - Using AppMessage for two way communication

Available in Pebble Kit 1.1Download from developer.getpebble.comRequires Pebble firmware 1.10.2

Page 3: Pebble Development - Using AppMessage for two way communication

New opportunities for developers!

•Watchfaces augmented with data from the internet• Remote controls for internet connected devices•Multi-player Pebble games• 4sq/Facebook/Yelp Check-in Apps• Sports/Weather/News/Traffic tickers• Emergency beacon activator• Deeper sports integration• Bitcoin price trackers

Page 4: Pebble Development - Using AppMessage for two way communication

TWO-WAY COMMUNICATION OVERVIEW

Page 5: Pebble Development - Using AppMessage for two way communication

Both sides can push updates

Page 6: Pebble Development - Using AppMessage for two way communication

Updates are key-value pairs (dictionary)

Page 7: Pebble Development - Using AppMessage for two way communication

{ /* icon */ 0: (uint8) 3, /* temp */ 1: “42°C”}

Key must be an integerValue can only be a string, bytes, int or int8

Page 8: Pebble Development - Using AppMessage for two way communication

App-channels identified by a shared UUID

42c86ea4-1c3e-4a07-b889-2cccca914198

Page 9: Pebble Development - Using AppMessage for two way communication

Acknowledgements provided by the framework

Page 10: Pebble Development - Using AppMessage for two way communication

Not all apps require two-way communication

Page 11: Pebble Development - Using AppMessage for two way communication

CURRENT LIMITATIONS

Page 12: Pebble Development - Using AppMessage for two way communication

Only the watchapp in the foreground can send and receive notifications

Page 13: Pebble Development - Using AppMessage for two way communication

Communication must be initiated by the phone

Only one 3rd-party app at a time

Apps need to be whitelisted by Pebble

Page 14: Pebble Development - Using AppMessage for two way communication

USING TWO-WAY COMMUNICATION

Page 15: Pebble Development - Using AppMessage for two way communication

Pebble

AppMessage PBWatch Intent Bus

AndroidiOS

Page 16: Pebble Development - Using AppMessage for two way communication

AppMessage Generic updates exchange

Page 20: Pebble Development - Using AppMessage for two way communication

DictionaryIterator *iter;

// Reserve the output bufferapp_message_out_get(&iter);

// Fill the bufferdict_write_*(iter, ...);dict_write_end(iter);

// Send the messageapp_message_out_send();

// Release the output bufferapp_message_out_release();

Send messages

AppMessage

Page 21: Pebble Development - Using AppMessage for two way communication

// Write data directlydict_write_data(iter, KEY_DATA, data, sizeof(data));dict_write_cstring(iter, KEY_STR, string);dict_write_int(iter, KEY_INT, value, sizeof(value) /*bytes*/, true /*signed*/);dict_write_uint8(iter, KEY_INT8, value8);

// Or use the Tuplet* constructsTuplet tuplet = TupletInteger(CMD_KEY, value);dict_write_tuplet(iter, &tuplet);

Writing into a dictionary

Dictionary

Page 22: Pebble Development - Using AppMessage for two way communication

Tuple *tuple = dict_read_first(&iter);while (tuple) { switch (tuple->key) { case KEY_TO_A_DATA_VALUE: foo(tuple->value->data, tuple->length); break; case KEY_TO_A_STRING_VALUE: bar(tuple->value->cstring); break; case KEY_TO_A_INT32_VALUE: foobar(tuple->value->int32); break; case KEY_TO_A_UINT8_VALUE: foobar(tuple->value->uint8); break; } tuple = dict_read_next(&iter);}

Reading from a dictionary

Dictionary

Page 23: Pebble Development - Using AppMessage for two way communication

dict_calc_buffer_size(2, sizeof(uint8_t), sizeof(char) * 8);

Evaluate the size of a dictionary

Dictionary

Page 24: Pebble Development - Using AppMessage for two way communication

size = 1 + nTuples * 7 + Size1 + Size2 + ...

Evaluate the size of a dictionary

Dictionary

Page 25: Pebble Development - Using AppMessage for two way communication

iOS Using AppMessage on iOS

Page 26: Pebble Development - Using AppMessage for two way communication

[watch appMessagesGetIsSupported:^(PBWatch *watch, BOOL isAppMessagesSupported) { if (isAppMessagesSupported) { uint8_t bytes[] = {0x42, 0xc8, /* ... */, 0x91, 0x41, 0x98}; NSData *uuid = [NSData dataWithBytes:bytes length:sizeof(bytes)]; [watch appMessagesSetUUID:uuid]; }}];

Find out if the watch supports two-way communicationRegister your channel with the shared UUID

iOS

Page 27: Pebble Development - Using AppMessage for two way communication

NSNumber *iconKey = @(0);NSNumber *temperatureKey = @(1);NSDictionary *update = @{ iconKey:[NSNumber numberWithUint8:weatherIconID], temperatureKey: @"42°C" };

[_targetWatch appMessagesPushUpdate:update onSent:^(PBWatch *watch, NSDictionary *update, NSError *error) { if (!error) { // :) } }];

Push updates to the watch

iOS

Page 28: Pebble Development - Using AppMessage for two way communication

id updateHandler;

/* ... */

[ourWatch appMessagesGetIsSupported: ^(PBWatch *watch, BOOL isAppMessagesSupported) {

if(updateHandler) [ourWatch appMessagesRemoveUpdateHandler:updateHandler]; updateHandler = [watch appMessagesAddReceiveUpdateHandler: ^BOOL(PBWatch *watch, NSDictionary *update) { /* process message */ return YES; } ]; }];

Receive updates from the watch

iOS

Page 29: Pebble Development - Using AppMessage for two way communication

Android Using AppMessage on Android

Page 30: Pebble Development - Using AppMessage for two way communication

if (PebbleKit.areAppMessagesSupported()) { PebbleDictionary data = new PebbleDictionary(); data.addUint8(ICON_KEY, (byte) weatherIconId); data.addString(TEMP_KEY, "42°C");

PebbleKit.sendDataToPebble(getApplicationContext(), UUID, data);}

Find out if the watch supports two-way communicationPush an update to the watch

Android

Page 31: Pebble Development - Using AppMessage for two way communication

public class MyDataReceiver extends PebbleDataReceiver { @Override public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) { /* Process data */ PebbleKit.sendAckToPebble(getApplicationContext(), transactionId); }}

/* ... */

PebbleDataReceiver receiver = new PebbleDataReceiver(UUID);PebbleKit.registerReceivedDataHandler(getApplicationContext(), receiver);

Register to receive updatesYou must acknowledge updates

Android

Page 32: Pebble Development - Using AppMessage for two way communication

Pebble

AppMessage PBWatch Intent Bus

AndroidiOS

AppSync

Page 33: Pebble Development - Using AppMessage for two way communication

AppSync Synchronize watch fields

Page 34: Pebble Development - Using AppMessage for two way communication

AppMessage

AppSync

A convenience layer on top of AppMessageMaintains and updates a Dictionary Provides a callback called whenever the Dictionary changes

Page 36: Pebble Development - Using AppMessage for two way communication

Tuplet initial_values[] = { TupletInteger(WEATHER_ICON_KEY, (uint8_t) 1), TupletCString(WEATHER_TEMPERATURE_KEY, "42°C"),};

Prepare the initial values of your data

Page 37: Pebble Development - Using AppMessage for two way communication

#include <...>

AppSync sync;uint8_t sync_buffer[32];

Reserve global memory to store a AppSync structReserve global memory to store your dictionary

Page 38: Pebble Development - Using AppMessage for two way communication

app_sync_init(&sync, sync_buffer, sizeof(sync_buffer), initial_values, ARRAY_LENGTH(initial_values), sync_tuple_changed_callback, sync_error_callback, NULL);

Initialize the synchronization

Page 39: Pebble Development - Using AppMessage for two way communication

void sync_tuple_changed_callback(const uint32_t key, const Tuple* new_tuple, const Tuple* old_tuple, void* context) { // Update your layers // Do not forget to call layer_mark_dirty()}

Process the first (and subsequent) update

Page 40: Pebble Development - Using AppMessage for two way communication

Tuplet new_tuples[] = { TupletInteger(WEATHER_ICON_KEY, (uint8_t) 3), TupletCString(WEATHER_TEMPERATURE_KEY, "73°C"),};

app_sync_set(&sync, new_tuples, 2);

You can update the value on the Watch sideThe callback will be called when the app has acknowledged

Page 41: Pebble Development - Using AppMessage for two way communication

Pebble

AppMessage PBWatch Intent Bus

AndroidiOS

AppSync

Page 42: Pebble Development - Using AppMessage for two way communication

THANK YOU!@pebbledev

Page 43: Pebble Development - Using AppMessage for two way communication

THANK YOU!@pebbledev