Titanium Pebble integration for iOS and Android
Download the latest distribution ZIP file in /dist
and consult the Titanium Documentation on how to install it, or simply use the gitTio CLI:
$ gittio install com.mcongrove.pebble
An example Titanium Mobile application and Pebble application are available for download.
Aside from the Quick Start guide below, you can also view the full module reference in the documentation.
Developed by Matthew Congrove with contributions from Jon Alter, Chris Bowley and Thomas Wilkinson.
Based on the TiPebble iOS module developed by Joe Beuckman.
Add this to your <ios><plist><dict>
section in tiapp.xml
:
<key>UISupportedExternalAccessoryProtocols</key>
<array>
<string>com.getpebble.public</string>
</array>
To keep the connection to the Pebble active while the application is running in background mode:
Register a BackgroundService
in Titanium. The background service script can require in the TitaniumPebble module and operate as normal. You'll also need to add the following to your tiapp.xml
:
<key>UIBackgroundModes</key>
<array>
<string>external-accessory</string>
</array>
Due to differences in how Android services work, Android requires more effort. You'll need to register and start a ServiceIntent
; do not specify an interval
to ensure the service only runs once. The background service script can require in the TitaniumPebble module and operate as normal.
However, because the service can run while the application is in the foreground, you need to unregister any event listeners in your application that you plan to use in the background service.
Import the TitaniumPebble module and provide your Pebble application's UUID:
var pebble = require("com.mcongrove.pebble");
// This demo UUID is from the Pebble documentation
pebble.setAppUUID("226834ae-786e-4302-a52f-6e7efc9f990b");
To connect to the Pebble:
pebble.connect({
success: function(_event) {
alert("Connected to Pebble");
},
error: function(_event) {
alert("Cannot Connect to Pebble");
}
});
Respond when the Pebble app connects/disconnects using the following code:
Note: The Pebble only sends connect/disconnect events when the watch pairing status changes or the watch enter/leaves the range of the phone. For instance, the connect event does not fire if the Pebble is already connected to your phone and you then launch the application.
function watchConnected(_event) {
alert("Watch Connected")
}
function watchDisconnected(_event) {
alert("Watch Disconnected");
}
pebble.addEventListener("watchConnected", watchConnected);
pebble.addEventListener("watchDisconnected", watchDisconnected);
To launch your Pebble application on the watch from your mobile application:
pebble.launchApp({
success: function(_event) {
alert("Pebble Application Launched");
},
error: function(_event) {
alert("Could Not Launch Pebble Application");
}
});
After you've connected, you can add an event listener to start watching for messages from the Pebble:
pebble.addEventListener("update", watchMessageReceived);
function watchMessageReceived(_message) {
alert("Message Received: " + _message.message);
}
To send messages from the Pebble, use the SDKs provided by Pebble. Here's a simple and incomplete example:
static void sendMessageToPhone() {
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
if(iter == NULL) {
return;
}
static char message[64] = "Hello, mobile app!";
dict_write_cstring(iter, 0, msg);
dict_write_end(iter);
app_message_outbox_send();
}
static void init() {
app_message_register_inbox_received(in_received_handler);
app_message_register_inbox_dropped(in_dropped_handler);
app_message_register_outbox_sent(out_sent_handler);
app_message_register_outbox_failed(out_failed_handler);
const uint32_t inbound_size = 64;
const uint32_t outbound_size = 64;
app_message_open(inbound_size, outbound_size);
sendMessageToPhone();
}
After you've connected, you can send messages from the phone to the Pebble:
pebble.sendMessage({
message: {
0: "Hi, Pebble!",
1: 12345
},
success: function(_event) {
alert("Message Sent");
},
error: function(_event) {
alert("Message Failed");
}
})
On the Pebble, use the SDKs provided by Pebble to receive the message. Here's a simple and incomplete example:
enum {
KEY_MSG_A = 0x0,
KEY_MSG_B = 0x1
};
static void in_received_handler(DictionaryIterator *iter, void *context) {
Tuple *message_a = dict_find(iter, KEY_MSG_A);
Tuple *message_b = dict_find(iter, KEY_MSG_B);
message_a ? persist_write_string(KEY_MSG_A, message_a->value->cstring) : false;
message_b ? persist_write_int(KEY_MSG_B, message_b->value->uint8) : false;
}
static void init() {
app_message_register_inbox_received(in_received_handler);
app_message_register_inbox_dropped(in_dropped_handler);
app_message_register_outbox_sent(out_sent_handler);
app_message_register_outbox_failed(out_failed_handler);
const uint32_t inbound_size = app_message_inbox_size_maximum();
const uint32_t outbound_size = app_message_outbox_size_maximum();
app_message_open(inbound_size, outbound_size);
}