Skip to content

Commit 37c6765

Browse files
committed
update callhandler example
1 parent d510d3f commit 37c6765

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

examples/ble_apple_notification_center_callhandler.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,73 @@
1212
import time
1313
import board
1414
import digitalio
15-
15+
import neopixel
1616
import adafruit_ble
1717
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
1818
import adafruit_ble_apple_notification_center as ancs
1919

20-
butA = digitalio.DigitalInOut(board.D4) # Circuit Playground Bluefruit A button
21-
butB = digitalio.DigitalInOut(board.D5) # Circuit Playground Bluefruit A button
22-
butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH
20+
# Circuit Playground Bluefruit buttons and LED setup
21+
butA = digitalio.DigitalInOut(board.D4) # CPB "A" button
22+
butB = digitalio.DigitalInOut(board.D5) # CPB "B" button
23+
butA.switch_to_input(digitalio.Pull.DOWN) # buttons are active HIGH
2324
butB.switch_to_input(digitalio.Pull.DOWN)
2425

26+
leds = neopixel.NeoPixel(board.D8, 10, brightness=0.1)
27+
28+
(coff, cred, cgrn, cblu, cgra) = (0x000000, 0xff0000, 0x00ff00, 0x0000ff, 0x111111 )
29+
leds_off = ( coff,coff,coff,coff,coff, coff,coff,coff,coff,coff )
30+
leds_idle = ( cgra,cgra,cgra,cgra,cgra, cgra,cgra,cgra,cgra,cgra )
31+
leds_incoming_call = ( coff,cgrn,cgrn,cgrn,coff, coff,cred,cred,cred,coff )
32+
leds_active_call = ( cgrn,coff,coff,coff,cgrn, cgrn,cred,cred,cred,cgrn )
33+
34+
print("starting...")
2535
radio = adafruit_ble.BLERadio() # pylint: disable=no-member
2636
a = SolicitServicesAdvertisement()
37+
#a.complete_name = "CIRPYCALLHANDLER" # this crashes things?
2738
a.solicited_services.append(ancs.AppleNotificationCenterService)
2839
radio.start_advertising(a)
2940

30-
while not radio.connected:
31-
pass
41+
last_display_time = time.monotonic()
3242

33-
print("connected")
43+
while True:
44+
while not radio.connected:
45+
print("not connected")
46+
time.sleep(1)
3447

35-
while radio.connected:
3648
for connection in radio.connections:
3749
if not connection.paired:
3850
connection.pair()
3951
print("paired")
4052

4153
ans = connection[ancs.AppleNotificationCenterService]
54+
4255
for notification in ans.wait_for_new_notifications():
43-
print("new notification:", notification)
56+
print("New Notification:\n- ", notification)
57+
58+
leds[:] = leds_idle
4459

45-
print("Notifications:", len(ans.active_notifications))
46-
for nid, n in ans.active_notifications.items():
47-
print(
48-
"uid:",
49-
n.id,
50-
"cat:",
51-
n.category_id,
52-
"title:",
53-
n.title,
54-
"msg:",
55-
n.message,
56-
)
57-
if n.category_id == 1: # incoming call, has positive & negative actions
60+
for notification in ans.active_notifications.values():
61+
# incoming call category, has positive & negative actions
62+
if notification.category_id == 1:
63+
leds[:] = leds_incoming_call
5864
if butA.value:
59-
print("accepting call")
60-
n.send_positive_action()
65+
print("Action: accepting call")
66+
notification.send_positive_action()
67+
time.sleep(1) # simple debounce
6168
if butB.value:
62-
print("declining call")
63-
n.send_negative_action()
64-
if n.category_id == 12: # active call, only has negative action
69+
print("Action: declining call")
70+
notification.send_negative_action()
71+
time.sleep(1) # simple debounce
72+
# active call category, only has negative action
73+
if notification.category_id == 12:
74+
leds[:] = leds_active_call
6575
if butB.value:
66-
print("hanging up call")
67-
n.send_negative_action()
68-
time.sleep(1)
76+
print("Action: hanging up call")
77+
notification.send_negative_action()
78+
time.sleep(1) # simple debounce
6979

70-
print("disconnected")
80+
if time.monotonic() - last_display_time > 3.0:
81+
last_display_time = time.monotonic()
82+
print("Current Notifications:", len(ans.active_notifications), time.monotonic())
83+
for nid, n in ans.active_notifications.items():
84+
print("- uid:",n.id, "catid:", n.category_id, "title:", n.title, "msg:", n.message)

0 commit comments

Comments
 (0)