From 8bd15cb29bfbec8b8b7a79c2b2a0dc3459c6175d Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Mon, 4 Apr 2022 17:16:39 -0700 Subject: [PATCH 1/4] add send_positive_action, send_negative_action, and callhandler action example --- adafruit_ble_apple_notification_center.py | 17 +++++ ...e_apple_notification_center_callhandler.py | 70 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 examples/ble_apple_notification_center_callhandler.py diff --git a/adafruit_ble_apple_notification_center.py b/adafruit_ble_apple_notification_center.py index e91eec9..3b18b33 100644 --- a/adafruit_ble_apple_notification_center.py +++ b/adafruit_ble_apple_notification_center.py @@ -70,6 +70,7 @@ def __get__(self, notification, cls): "BusinessAndFinance", "Location", "Entertainment", + "ActiveCall", ) @@ -140,6 +141,22 @@ def __init__( self.control_point = control_point self.data_source = data_source + def send_positive_action(self): + """Sends positive action on this notification. For example, to accept an IncomingCall.""" + cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION, + uid = self.id + action_id = 0 # ANCS_ACTION_POSITIVE + buffer = struct.pack( " Date: Mon, 4 Apr 2022 17:21:32 -0700 Subject: [PATCH 2/4] fix line endings --- ...e_apple_notification_center_callhandler.py | 140 +++++++++--------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/examples/ble_apple_notification_center_callhandler.py b/examples/ble_apple_notification_center_callhandler.py index e9c5b9e..36b0c7e 100755 --- a/examples/ble_apple_notification_center_callhandler.py +++ b/examples/ble_apple_notification_center_callhandler.py @@ -1,70 +1,70 @@ -# SPDX-FileCopyrightText: 2022 Tod Kurt for Adafruit Industries -# SPDX-License-Identifier: MIT - -""" -This example solicits notifications from Apple devices paired with it, -detecting specifically the IncomingCall and ActiveCall notification categories -and sending back Positive Actions to accept calls or Negative Actions to -decline or hang up calls. It also shows initiating pairing, prints existing -notifications and prints any new ones as they arrive. -""" - -import time -import board -import digitalio - -import adafruit_ble -from adafruit_ble.advertising.standard import SolicitServicesAdvertisement -import adafruit_ble_apple_notification_center as ancs - -butA = digitalio.DigitalInOut(board.D4) # Circuit Playground Bluefruit A button -butB = digitalio.DigitalInOut(board.D5) # Circuit Playground Bluefruit A button -butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH -butB.switch_to_input(digitalio.Pull.DOWN) - -radio = adafruit_ble.BLERadio() # pylint: disable=no-member -a = SolicitServicesAdvertisement() -a.solicited_services.append(ancs.AppleNotificationCenterService) -radio.start_advertising(a) - -while not radio.connected: - pass - -print("connected") - -while radio.connected: - for connection in radio.connections: - if not connection.paired: - connection.pair() - print("paired") - - ans = connection[ancs.AppleNotificationCenterService] - for notification in ans.wait_for_new_notifications(): - print("new notification:", notification) - - print("Notifications:", len(ans.active_notifications)) - for nid, n in ans.active_notifications.items(): - print( - "uid:", - n.id, - "cat:", - n.category_id, - "title:", - n.title, - "msg:", - n.message, - ) - if n.category_id == 1: # incoming call, has positive & negative actions - if butA.value: - print("accepting call") - n.send_positive_action() - if butB.value: - print("declining call") - n.send_negative_action() - if n.category_id == 12: # active call, only has negative action - if butB.value: - print("hanging up call") - n.send_negative_action() - time.sleep(1) - -print("disconnected") +# SPDX-FileCopyrightText: 2022 Tod Kurt for Adafruit Industries +# SPDX-License-Identifier: MIT + +""" +This example solicits notifications from Apple devices paired with it, +detecting specifically the IncomingCall and ActiveCall notification categories +and sending back Positive Actions to accept calls or Negative Actions to +decline or hang up calls. It also shows initiating pairing, prints existing +notifications and prints any new ones as they arrive. +""" + +import time +import board +import digitalio + +import adafruit_ble +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement +import adafruit_ble_apple_notification_center as ancs + +butA = digitalio.DigitalInOut(board.D4) # Circuit Playground Bluefruit A button +butB = digitalio.DigitalInOut(board.D5) # Circuit Playground Bluefruit A button +butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH +butB.switch_to_input(digitalio.Pull.DOWN) + +radio = adafruit_ble.BLERadio() # pylint: disable=no-member +a = SolicitServicesAdvertisement() +a.solicited_services.append(ancs.AppleNotificationCenterService) +radio.start_advertising(a) + +while not radio.connected: + pass + +print("connected") + +while radio.connected: + for connection in radio.connections: + if not connection.paired: + connection.pair() + print("paired") + + ans = connection[ancs.AppleNotificationCenterService] + for notification in ans.wait_for_new_notifications(): + print("new notification:", notification) + + print("Notifications:", len(ans.active_notifications)) + for nid, n in ans.active_notifications.items(): + print( + "uid:", + n.id, + "cat:", + n.category_id, + "title:", + n.title, + "msg:", + n.message, + ) + if n.category_id == 1: # incoming call, has positive & negative actions + if butA.value: + print("accepting call") + n.send_positive_action() + if butB.value: + print("declining call") + n.send_negative_action() + if n.category_id == 12: # active call, only has negative action + if butB.value: + print("hanging up call") + n.send_negative_action() + time.sleep(1) + +print("disconnected") From 37c6765de2e20c3ce268a2ff5e88bed58ac8b7cc Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Tue, 5 Apr 2022 16:10:11 -0700 Subject: [PATCH 3/4] update callhandler example --- ...e_apple_notification_center_callhandler.py | 76 +++++++++++-------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/examples/ble_apple_notification_center_callhandler.py b/examples/ble_apple_notification_center_callhandler.py index 36b0c7e..a7100af 100755 --- a/examples/ble_apple_notification_center_callhandler.py +++ b/examples/ble_apple_notification_center_callhandler.py @@ -12,59 +12,73 @@ import time import board import digitalio - +import neopixel import adafruit_ble from adafruit_ble.advertising.standard import SolicitServicesAdvertisement import adafruit_ble_apple_notification_center as ancs -butA = digitalio.DigitalInOut(board.D4) # Circuit Playground Bluefruit A button -butB = digitalio.DigitalInOut(board.D5) # Circuit Playground Bluefruit A button -butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH +# Circuit Playground Bluefruit buttons and LED setup +butA = digitalio.DigitalInOut(board.D4) # CPB "A" button +butB = digitalio.DigitalInOut(board.D5) # CPB "B" button +butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH butB.switch_to_input(digitalio.Pull.DOWN) +leds = neopixel.NeoPixel(board.D8, 10, brightness=0.1) + +(coff, cred, cgrn, cblu, cgra) = (0x000000, 0xff0000, 0x00ff00, 0x0000ff, 0x111111 ) +leds_off = ( coff,coff,coff,coff,coff, coff,coff,coff,coff,coff ) +leds_idle = ( cgra,cgra,cgra,cgra,cgra, cgra,cgra,cgra,cgra,cgra ) +leds_incoming_call = ( coff,cgrn,cgrn,cgrn,coff, coff,cred,cred,cred,coff ) +leds_active_call = ( cgrn,coff,coff,coff,cgrn, cgrn,cred,cred,cred,cgrn ) + +print("starting...") radio = adafruit_ble.BLERadio() # pylint: disable=no-member a = SolicitServicesAdvertisement() +#a.complete_name = "CIRPYCALLHANDLER" # this crashes things? a.solicited_services.append(ancs.AppleNotificationCenterService) radio.start_advertising(a) -while not radio.connected: - pass +last_display_time = time.monotonic() -print("connected") +while True: + while not radio.connected: + print("not connected") + time.sleep(1) -while radio.connected: for connection in radio.connections: if not connection.paired: connection.pair() print("paired") ans = connection[ancs.AppleNotificationCenterService] + for notification in ans.wait_for_new_notifications(): - print("new notification:", notification) + print("New Notification:\n- ", notification) + + leds[:] = leds_idle - print("Notifications:", len(ans.active_notifications)) - for nid, n in ans.active_notifications.items(): - print( - "uid:", - n.id, - "cat:", - n.category_id, - "title:", - n.title, - "msg:", - n.message, - ) - if n.category_id == 1: # incoming call, has positive & negative actions + for notification in ans.active_notifications.values(): + # incoming call category, has positive & negative actions + if notification.category_id == 1: + leds[:] = leds_incoming_call if butA.value: - print("accepting call") - n.send_positive_action() + print("Action: accepting call") + notification.send_positive_action() + time.sleep(1) # simple debounce if butB.value: - print("declining call") - n.send_negative_action() - if n.category_id == 12: # active call, only has negative action + print("Action: declining call") + notification.send_negative_action() + time.sleep(1) # simple debounce + # active call category, only has negative action + if notification.category_id == 12: + leds[:] = leds_active_call if butB.value: - print("hanging up call") - n.send_negative_action() - time.sleep(1) + print("Action: hanging up call") + notification.send_negative_action() + time.sleep(1) # simple debounce -print("disconnected") + if time.monotonic() - last_display_time > 3.0: + last_display_time = time.monotonic() + print("Current Notifications:", len(ans.active_notifications), time.monotonic()) + for nid, n in ans.active_notifications.items(): + print("- uid:",n.id, "catid:", n.category_id, "title:", n.title, "msg:", n.message) From 78e3a12c30a42c4597b62e765e6566eefc586f67 Mon Sep 17 00:00:00 2001 From: Tod Kurt Date: Tue, 5 Apr 2022 16:58:03 -0700 Subject: [PATCH 4/4] appease pre-commit gods --- adafruit_ble_apple_notification_center.py | 12 +++--- ...e_apple_notification_center_callhandler.py | 37 +++++++++++++------ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/adafruit_ble_apple_notification_center.py b/adafruit_ble_apple_notification_center.py index 3b18b33..5fc3b73 100644 --- a/adafruit_ble_apple_notification_center.py +++ b/adafruit_ble_apple_notification_center.py @@ -143,18 +143,18 @@ def __init__( def send_positive_action(self): """Sends positive action on this notification. For example, to accept an IncomingCall.""" - cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION, + cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION, uid = self.id - action_id = 0 # ANCS_ACTION_POSITIVE - buffer = struct.pack( " 3.0: last_display_time = time.monotonic() - print("Current Notifications:", len(ans.active_notifications), time.monotonic()) + print( + "Current Notifications:", + len(ans.active_notifications), + time.monotonic(), + ) for nid, n in ans.active_notifications.items(): - print("- uid:",n.id, "catid:", n.category_id, "title:", n.title, "msg:", n.message) + print( + "- uid:", + n.id, + "catid:", + n.category_id, + "title:", + n.title, + "msg:", + n.message, + )