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.#define KEY_CALLS_COUNT 41 static AppSync s_sync; static uint8_t s_sync_buffer[32]; /* ... */ 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(); } /* ... */ 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) { // snprintf(s_count_buffer, sizeof(s_count_buffer), "%d", (int)new_tuple->value->int32); // layer_mark_dirty(layer); } static void sync_error_handler(DictionaryResult dict_error, AppMessageResult app_message_error, void *context) { APP_LOG(APP_LOG_LEVEL_ERROR, "sync error!"); } /* ... */ 
dependencies { compile 'com.getpebble:pebblekit:2.6.0' } repositories { mavenCentral() maven { url "https://oss.sonatype.org/content/groups/public/" } } public class NotificationListener extends NotificationListenerService { @Override public void onNotificationPosted( StatusBarNotification sbn) { this.getMissedCalls(); } @Override public void onNotificationRemoved( StatusBarNotification sbn) { this.getMissedCalls(); } } public class NotificationListener extends NotificationListenerService { // int missedCallsCount = 0; /*...*/ void getMissedCalls() { int tCount = 0; for (StatusBarNotification notif : this.getActiveNotifications()) { if (notif.getPackageName().equals("com.android.phone")) { String extras_text = notif.getNotification().extras.getString(Notification.EXTRA_TEXT); if (extras_text.indexOf(" :") != -1) { tCount = Integer.parseInt(extras_text.split(":")[1].trim()); } } } if (tCount != missedCallsCount) { missedCallsCount = tCount; this.sendMissedCalls(missedCallsCount); } } } 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); } /*...*/ } <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> 

Source: https://habr.com/ru/post/249167/
All Articles