|
| 1 | +"""This demo shows the latest notification from a connected Apple device on the REPL and Gizmo""" |
| 2 | +import time |
| 3 | +import adafruit_ble |
| 4 | +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement |
| 5 | +from adafruit_ble_apple_notification_center import AppleNotificationCenterService |
| 6 | +from adafruit_gizmo import tft_gizmo |
| 7 | + |
| 8 | +# This is a whitelist of apps to show notifications from. |
| 9 | +#APPS = ["com.tinyspeck.chatlyio", "com.atebits.Tweetie2"] |
| 10 | +APPS = [] |
| 11 | + |
| 12 | +DELAY_AFTER_PRESS = 15 |
| 13 | +DEBOUNCE = 0.1 |
| 14 | + |
| 15 | +def find_connection(): |
| 16 | + for connection in radio.connections: |
| 17 | + if AppleNotificationCenterService not in connection: |
| 18 | + continue |
| 19 | + if not connection.paired: |
| 20 | + connection.pair() |
| 21 | + return connection, connection[AppleNotificationCenterService] |
| 22 | + return None, None |
| 23 | + |
| 24 | +# Start advertising before messing with the display so that we can connect immediately. |
| 25 | +radio = adafruit_ble.BLERadio() |
| 26 | +advertisement = SolicitServicesAdvertisement() |
| 27 | +advertisement.complete_name = "CIRCUITPY" |
| 28 | +advertisement.solicited_services.append(AppleNotificationCenterService) |
| 29 | + |
| 30 | +display = tft_gizmo.TFT_Gizmo() |
| 31 | + |
| 32 | +current_notification = None |
| 33 | +new_ids = [] |
| 34 | +displayed_ids = [] |
| 35 | +active_connection, notification_service = find_connection() |
| 36 | +while True: |
| 37 | + if not active_connection: |
| 38 | + radio.start_advertising(advertisement) |
| 39 | + |
| 40 | + while not active_connection: |
| 41 | + print("waiting for connection...") |
| 42 | + active_connection, notification_service = find_connection() |
| 43 | + time.sleep(0.1) |
| 44 | + |
| 45 | + while active_connection.connected: |
| 46 | + current_notifications = notification_service.active_notifications |
| 47 | + for notification_id in current_notifications: |
| 48 | + if notification_id in displayed_ids: |
| 49 | + continue # already seen! |
| 50 | + notification = current_notifications[notification_id] |
| 51 | + print('-'*36) |
| 52 | + category = str(notification).split(" ", 1)[0] |
| 53 | + print("Msg #%d - Category %s" % (notification.id, category)) |
| 54 | + print("From app:", notification.app_id) |
| 55 | + if notification.title: |
| 56 | + print("Title:", notification.title) |
| 57 | + if notification.subtitle: |
| 58 | + print("Subtitle:", notification.subtitle) |
| 59 | + if notification.message: |
| 60 | + print("Message:", notification.message) |
| 61 | + displayed_ids.append(id) |
| 62 | + active_connection = None |
| 63 | + notification_service = None |
0 commit comments