
Thanks to the official mobile application, Pebble copes with informing about the status of your smartphone - show incoming messages, call information and other notifications. But what to do if such a necessary "trifle", such as the condition of a smartphone's battery, the number of unread SMS and e-mail, is not available for use in your applications on the clock? Option to implement it yourself.
And so, on how to use PebbleKit Android to integrate Pebble and Android applications using the example of
notification of the number of missed calls : some code, translation of extracts from the
documentation [1], and very few pictures.
Thesis how the interaction of the mobile application and the watchappp works:
- companion application recognizes, connects and communicates with Pebble through the PebbleKit library;
- The watchapp (watchface) and the mobile application exchange messages bilaterally, the channel is identified from both sides by the Pebble application UUID;
- The exchange between the Pebble application and the mobile application is carried out by Dictionary objects [2] .
To solve the task, to display a notification on the clock about the number of missed calls, you need:
- Pebble-application that can receive, process and display information from the companion application;
- Android application that tracks calls and sends the number of unanswered to our application on the clock.
')
Watchapp
In the Pebble-application, in the same way as in the case of PebbleKit JS, the
AppMessage API mechanism is used
[3] .
Since our application does not initiate a communication session itself, but only waits for a message from the smartphone, I will use the
Synchronizing App UI [4] - an auxiliary layer on top of AppMessage, which simplifies the synchronization of values ​​between the watchapp and the mobile device. Only two callbacks are used, one of which in the case when the predefined value changes, and the other if an error occurs.
We define the key, AppSync and buffer for synchronizing the tuple:
#define KEY_CALLS_COUNT 41 static AppSync s_sync; static uint8_t s_sync_buffer[32];
The initialization of the synchronization mechanism and the initial value of the tuple will be put into a separate function:
static void start_sync() { app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); // (0 ) Tuplet initial_values[] = { TupletInteger(KEY_CALLS_COUNT, 0), }; app_sync_init(&s_sync, s_sync_buffer, sizeof(s_sync_buffer), initial_values, ARRAY_LENGTH(initial_values), sync_changed_handler, sync_error_handler, NULL); } static void init(void) { start_sync(); }
Define callbacks:
static char s_count_buffer[4]; static void sync_changed_handler(const uint32_t key, const Tuple *new_tuple, const Tuple *old_tuple, void *context) {
NB In this case, the key used to exchange the value does not require registration in appinfo.json.If we start our simple application now, there will be a single indicator on the screen (0 missed calls):

Android application
You can get the number of missed calls in Android in different ways, in this example I will take it from the "curtain" with notifications - Notification Area.
The application itself consists of two classes: MainActivity for entering the UUID watchapp that will receive data and the NotificationListener service that monitors notifications.
NB Access to the Notification Area via the NotificationListenerService appeared in Android 4.3. As the additional metadata Notification.extras is used, the example will be guaranteed to work only on Android 4.4+.For Android Studio, the addition of PebbleKit is done via a gradle file.
Add to
app / build.gradle :
dependencies { compile 'com.getpebble:pebblekit:2.6.0' } repositories { mavenCentral() maven { url "https://oss.sonatype.org/content/groups/public/" } }
After synchronization, PebbleKit can be used in your project.
To access notifications, I use the NotificationListener class, inherited from NotificationListenerService with overridden onNotificationPosted () and onNotificationRemoved () methods:
public class NotificationListener extends NotificationListenerService { @Override public void onNotificationPosted( StatusBarNotification sbn) { this.getMissedCalls(); } @Override public void onNotificationRemoved( StatusBarNotification sbn) { this.getMissedCalls(); } }
When adding or deleting a notification, all active calls are viewed and if there is information about missed calls, the number of them is sent to the application on the clock:
public class NotificationListener extends NotificationListenerService {
To send data to the clock, we define the key, UUID, form the dictionary and use the sendDataToPebble method:
public class NotificationListener extends NotificationListenerService { UUID APPS_UUID; private static final int CALLS_KEY = 41; public void sendMissedCalls(int missedCalls) { APPS_UUID = UUID.fromString(this.getUUID()); PebbleDictionary data = new PebbleDictionary(); data.addUint32(CALLS_KEY, missedCalls); PebbleKit.sendDataToPebble(getApplicationContext(), APPS_UUID, data); } }
To provide the application with access rights to notifications, you need to add to the manifest:
<service android:name=".NotificationListener" android:label="@string/app_name" android:permission= "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name= "android.service.notification.NotificationListenerService" /> </intent-filter> </service>
Note, after installing the application, you must enable the service, it will not work without authorization. The setting is located on the path “Settings” -> “Security” -> “Notification access”. In my Russian locale, I have “Settings” -> “Privacy” -> “Access to notifications”.

We start the android application, indicate the UUID of the Pebble application in it, call and do not answer, see how the notification changes on the clock:

Sources:
notice watchapp -
pbwPebbleNotify (Android Studio project) -
apk1. Pebble Developers // Mobile App Developer Guide2. Pebble Developers // Dictionary3. Pebble Developers // App Communication4. Pebble Developers // Synchronizing App UI