Create native applications with PebbleKit support using Cordova.
No need for Java or Objective C to create mobile apps that communicate with your Pebble application. Use one framework and the vast set of existing Cordova plugins to extend the functionality of your watch app/face.
- Ensure
adb
is in your PATH. (Android only). - Install Cordova,
npm install -g cordova
.
To add PebbleKit support to an existing Cordova project, run:
$ cordova plugin add cordova-plugin-pebblekit
note If supporting iOS, the following steps are required to build the application for the first time.
- Open up XCode and open your project's
xcodeproj
file (<project-directory>/platforms/ios/<project-name>.xcodeproj
). - Click on your project's name on the left pane.
- In the
General
tab,Linked Frameworks and Libraries
, highlightPebbleKit.Framework
and click-
to remove it. - Now drag
PebbleKit.Framework
from theFrameworks
folder in the left pane into theEmbedded Binaries
section.
If you run into an Invalid Provisioning Profile
error:
- Navigate to
Build Phases
in XCode again, as described in the above steps. - Tick the checkbox next to
Code Sign On Copy
forPebbleKit.Framework
.
setup
isWatchConnected
registerPebbleConnectedReceiver
registerPebbleDisconnectedReceiver
unregisterPebbleConnectedReceiver
unregisterPebbleDisconnectedReceiver
startAppOnPebble
closeAppOnPebble
areAppMessagesSupported
sendAppMessage
registerReceivedDataHandler
unregisterReceivedDataHandler
Users must wait for the success callback of this function to be called before calling the following methods.
startAppOnPebble
closeAppOnPebble
areAppMessagesSupported
sendAppMessage
registerReceivedDataHandler
Arguments
uuid
- The UUID of the Pebble application to be interacted with.successCallback
- A callback which is called once the connection is setup on the iOS device. Called immediately if running on an Android device.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.setup(uuid, function () {
console.log('ready');
}, function (err) {
// error
});
Determine if a Pebble watch is currently connected to the phone.
Arguments
successCallback
- A callback which is called when the status of the connection has been determined.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.isWatchConnected(function(connected) {
console.log('Watch is connected', connected);
}, function(err) {
// error
});
Register to be notified when a Pebble has been connected to the phone.
Arguments
successCallback
- A callback which is called when the watch is connected.errorCallback
- Optional A callback which is called if an error has occurred.keepAlive
- Optional set totrue
to keep the receiver alive after the phone app has gone to in to the background. (See here for more detail).
Example
var keepAlive = false;
window.pebblekit.registerPebbleConnectedReceiver(function() {
// pebble connected
}, function(err) {
// error
}, keepAlive);
Register to be notified when a Pebble has been disconnected from the phone.
Arguments
successCallback
- A callback which is called when the watch is disconnected.errorCallback
- Optional A callback which is called if an error has occurred.keepAlive
- Optional set totrue
to keep the receiver alive after the phone app has gone to in to the background. (See here for more detail).
Example
var keepAlive = false;
window.pebblekit.registerPebbleDisconnectedReceiver(function() {
// pebble disconnected
}, function(err) {
// error
}, keepAlive);
Stop being notified about when a pebble is connected to the phone.
Arguments
successCallback
- Optional A callback which is called once the receiver has been unregistered.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.unregisterPebbleConnectedReceiver(function() {
// receiver unregistered
}, function (err) {
// error
});
Stop being notified about when a pebble is disconnected to the phone.
Arguments
successCallback
- Optional A callback which is called once the receiver has been unregistered.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.unregisterPebbleDisconnectedReceiver(function() {
// receiver unregistered
}, function (err) {
// error
});
Start an application on the pebble with the specified uuid
.
Arguments
uuid
- The UUID of the application to start on the watch.successCallback
- Optional A callback which is called once the app has been started.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.startAppOnPebble('ebc92429-483e-4b91-b5f2-ead22e7e002d', function() {
// app started on pebble
}, function (err) {
// error
});
Close an app on the watch with the specified uuid
.
Arguments
uuid
- The UUID of the application to close on the watch.successCallback
- Optional A callback which is called once the app has been started.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.closeAppOnPebble('ebc92429-483e-4b91-b5f2-ead22e7e002d', function() {
// App closed on pebble
}, function (err) {
// error
});
Determine whether or not the currently connected watch supports
AppMessage
.
Arguments
successCallback
- A callback which is called, containing the result of whetherAppMessage
is supported.errorCallback
- Optional A callback which is called if an error has occurred.
Example
window.pebblekit.areAppMessagesSupported(function(supported) {
console.log('AppMessages supported:', supported);
}, function(err) {
// error
});
Send an AppMessage
to the watch.
Arguments
uuid
- The UUID of the pebble application theAppMessage
should be sent to.data
- AnObject
containing data to send to the watch.ackHandler
- A callback that is called if theAppMessage
isack
ed.nackHandler
- A callback that is called if theAppMessage
isnack
ed.errorCallback
- Optional A callback that is called if there was a problem sending theAppMessage
.
note - Depending on the type of the item in the object to be sent, the C
app will be able to read the value (from the Tuple.value
union) according to
the table below:
note - If running on the Android platform, a transactionId
will be passed
to the successCallback
and errorCallback
, representing the transaction id
of that particular app message. This value will be -1
if on the iOS
platform.
JS Type | C Type |
---|---|
String | cstring |
Number | int32 |
Array | data |
Boolean | int16 |
note - If running on the iOS platform, the Boolean
type will actually be
int32
type on the C side, however, you may still interpret it as int16
in
the C code without any problems.
Example
Sending from the Cordova JS side:
var uuid = "ebc92429-483e-4b91-b5f2-ead22e7e002d";
var data = {
'0': 'String value',
'1': 42,
'2': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] // Byte array,
'3': true
}
window.pebblekit.sendAppMessage(uuid, data, function() {
// app message has been acked
}, function() {
// app message has been nacked
}, function(err) {
// err
})
Reading the data on the C side:
#define APP_KEY_STRING_VALUE 0
static void inbox_received_callback(DictionaryIterator *iter, void *context) {
Tuple *tuple;
tuple = dict_find(iter, APP_KEY_STRING_VALUE)
if (tuple) {
char* value_string = tuple->value->cstring;
APP_LOG(APP_LOG_LEVEL_DEBUG, value_string); // 'String value'
}
}
To read more about reading AppMessage
on the C side, see the
documentation
Register a callback for when the specified watch app with the given uuid
sends an AppMessage
.
Arguments
uuid
- The UUID of the pebble app in which to be receiving messages from.successCallback
- A callback which is called when a new app message is received from the application with the givenuuid
.errorCallback
- Optional A callback which is called if an error has occurred.keepAlive
- Optional set totrue
to keep the receiver alive after the phone app has gone to in to the background. (See here for more detail).
note - Acking and Nacking the message is taken care of for you. If sending data, the base64 representation will be received on the Cordova JS side.
Example
Sending from the C side:
typedef enum {
AppKeyInt = 0,
AppKeyString = 1,
AppKeyData = 2
} AppKeys
static void send_app_msg() {
DictionaryIterator *out_iter;
AppMessageResult result = app_message_outbox_begin(&out_iter);
if (result != APP_MSG_OK) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Error preparing outbox: %d", (int) result);
return;
}
int value_int = 42;
char* value_string = "Message from C";
uint8_t value_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 9};
dict_write_int(out_iter, AppKeyInt, &value, sizeof(int), true);
dict_write_cstring(out_iter, AppKeyString, "message from C");
dict_write_data(out_iter, AppKeyData, data, sizeof(data));
result = app_message_outbox_send();
if (result != APP_MSG_OK) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Error sending the outbox: %d", (int) result);
}
}
Receiving on the Cordova JS side:
var uuid = "ebc92429-483e-4b91-b5f2-ead22e7e002d";
var keepAlive = false;
window.pebblekit.registerReceivedDataHandler(uuid, function(data) {
console.log('Received data', JSON.stringify(data));
/*
Received data {
"0": 0,
"1": "message from C",
"2": "AAECAwQFBgcICQ=="
}
*/
}, function (err) {
// error
}, keepAlive);
Stop listening for AppMessage
sent from the watch.
Arguments
successCallback
- A callback that is called once the data handler has been unregistered.errorCallback
- Optional A callback that is called if an error has occurred.
Example
window.pebblekit.unregisterReceivedDataHandler(function() {
// received data handler unregistered
}, function (err) {
// error
});
Some functions have a keepAlive
parameter. By default, this plugin will take
care of unregistering receivers for you when the app goes into the background
in order to avoid memory leaks.
If you set this option to true
, you should make sure to call the
corresponding unregisterX()
function when you are done with it. The receiver
will stay active until the application is killed by the OS. This feature is
only available for Android.
You must first install the dependencies.
cd example/cordova
cordova platform add android
and/orcordova platform add ios
cordova plugin add cordova-plugin-pebblekit
cordova plugin add cordova-plugin-calendar
cordova run android --device
orcordova run ios --device
note Running make
with no arguments will overwrite the source files of
the plugin with the corresponding files in the respective platform build
directory.
cd example/pebble
.pebble build && pebble install --phone <PHONE_IP>
For more help, see the documentation.
You can use console.log() statements and breakpoints for debugging your Cordova JS on iOS and Android.
Your phone needs to be connected via USB with USB Debugging
enabled. Launch
your application: cordova run android --device
. Open Google Chrome on your
computer and go to 'Inspect Devices' chrome://inspect/#devices
and select
your Cordova application. You should be able to inspect your code and set
breakpoints etc.
Your iPhone needs to be unlocked and connected via USB. Now go to Settings > Safari > Advanced
and enable Web Inspector
. Launch your application: cordova run ios --device
. Open Safari on
your computer and select your iPhone from the Develop
menu. You should be able to inspect your
code and set breakpoints etc.