Search code examples
javascriptandroidpebble-watchpebble-sdkcloudpebble

Pebble communication methods


I've been experimenting with pebble for a day or two now and I've reached a bit of a standstill. That is, I can't seem to figure out how certain things are related. What I'm trying to do is get data from an app on Android to be sent to pebble, and for pebble to then do something productive with that data.

I've realized that you use the PebbleKit API within an android application in order to communicate with the pebble, using things such as sendDataToPebble(). After that, though, I'm not quite sure what to do.

Using cloudpebble.net, I've created an application in C using the pebble SDK to acknowledge the data (using the handlers as suggested on their website). However, I've recently discovered that there can also be javascript in this entire mix, and I just don't know how to interface with the js.

Is it possible to have all ack/nack of data, and subsequent processing/displaying of data done entirely through javascript, avoiding the C entirely, or do i need to receive the data in c, and then send it to javascript some how for it to use (which I don't currently know how to do).

I believe there is some sort of event that is triggered (appmessage, I think), when data is received by the JS app, however, I think that is only from the C app?

Thus, mostly, my confusion lies in how JS falls into the entire mix. If someone could clear that up for me I would sincerely appreciate it.

EDIT: I should add that I'm trying to create an app using pebble.js in cloudpebble.net.

Thanks!


Solution

  • Note: Some of the links below go to Pebble API documentation, which at the time of writing is only accessible if you're logged into the developer site with your Pebble account.

    Getting started with Pebble development can be a little overwhelming. There are lots of moving parts, some of which you'll need and some of which you won't. Here's a quick overview of the major components:

    1. An app written in C. You'll definitely need to write one of these. (Well, almost definitely. See the note about SimplyJS below.)

      This is what runs on the actual watch. Here you'll likely create windows, menus, maybe do some work with graphics. You'll probably set up click handlers to handle button presses.

      The watch itself can't do very many interesting things. For example, it can't make any network requests, so no communication with the web. It can't do geolocation itself. You could build a completely self-contained watchface or timer or simple game though.

    2. Unless your app runs entirely on the watch, you'll also need one of the following:

      • An Android app written in Java, using PebbleKit for Android.
      • An iOS app written in Objective C, using PebbleKit for iOS.

        I don't have any experience with either PebbleKit for Android or PebbleKit for iOS, but they do both have official API documentation:

        Neither of these can be used from CloudPebble.net.

      • A JavaScript app written using PebbleKitJS, which works on both Android and iOS.

        Because this option lets developers target both mobile platforms, it's my favourite option. My sense is that many other developers agree. Unless you need to access things outside of what PebbleKitJS offers, I'd recommend using it.

        The JavaScript code here gets executed from within the official Pebble application on your phone. Essentially, the Pebble app has a sandboxed JavaScript environment for each watch app. If you go this route, you do not build a companion Android or iOS app. The official Pebble app effectively becomes your companion app.

        (Note that on iOS the JavaScript code must actually be bundled inside the official Pebble app. This generates a roughly 5-6 day period after releasing your app where iOS users will get the message that it's "available soon", while Android users will be able to use it immediately.)

    3. Finally, if you really don't want to use C, you can write a Pebble app purely in JavaScript using Simply.js (an unsupported, third-party tool). I've never used this, primarly because it seems that you can only have one Simply.js app running on your watch at once:

      How Do I Use This?

      You'll need a Pebble with OS 2.0.

      • Write a Simply.js script and host it online.
      • Download Simply.js v0.3.2 onto your Pebble.
      • Go to "My Pebble" in the new official Pebble Phone App.
      • Tap the Simply.js settings gear.
      • Place the URL to your script and hit Save.† Your script should immediately run.
      • Play with your Pebble!

      †If you're hosting your script on Github, use the raw URL to your script.

    To make things even more confusing, with any of the three phone APIs you can use one of two communication APIs:

    1. AppMessage is the foundational communication API that developers can use:

      AppMessage is a bi-directional messaging subsystem that enables communication between phone apps and Pebble watchapps. This is accomplished by allowing phone and watchapps to exchange arbitrary sets of key/value pairs. The key/value pairs are stored in the form of a Dictionary, the layout of which is left for the application developer to define.

      It works reasonably well, but it can be a bit tricky to get working at times. If you're working with PebbleKitJS, use console.log freely as there is no debugger.

    2. AppSync is a higher level library that claims to be for UI synchronization:

      AppSync is a convenience layer that resides on top of AppMessage, and serves as a UI synchronization layer for AppMessage. In so doing, AppSync makes it easier to drive the information displayed in the watchapp UI with messages sent by a phone app.

      AppSync maintains and updates a Dictionary, and provides your app with a callback (AppSyncTupleChangedCallback) routine that is called whenever the Dictionary changes and the app's UI is updated. Note that the app UI is not updated automatically. To update the UI, you need to implement the callback.

      I haven't used it yet, but I'm about to try migrating an app from AppMessage to AppSync. It looks useful even when you're not doing UI synchronization.

    Along with the official guides and API docs, Pebble provides a number of useful sample apps in the SDK. Since you seem to be interested in the JavaScript option, the most interesting are probably

    • the stock quotes app, located at Examples/pebblekit-js/quotes/, and
    • the weather app, located at Examples/pebblekit-js/weather/.

    Check out each app's src/<appname>.c and src/js/pebble-js-app.js files.