forked from hbldh/bleak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable_notifications.py
48 lines (34 loc) · 1.16 KB
/
enable_notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
"""
Notifications
-------------
Example showing how to add notifications to a characteristic and handle the responses.
Updated on 2019-07-03 by hbldh <[email protected]>
"""
import sys
import asyncio
import platform
from bleak import BleakClient
# you can change these to match your device or override them from the command line
CHARACTERISTIC_UUID = "f000aa65-0451-4000-b000-000000000000"
ADDRESS = (
"24:71:89:cc:09:05"
if platform.system() != "Darwin"
else "B9EA5233-37EF-4DD6-87A8-2A875E821C46"
)
def notification_handler(sender, data):
"""Simple notification handler which prints the data received."""
print("{0}: {1}".format(sender, data))
async def main(address, char_uuid):
async with BleakClient(address) as client:
print(f"Connected: {client.is_connected}")
await client.start_notify(char_uuid, notification_handler)
await asyncio.sleep(5.0)
await client.stop_notify(char_uuid)
if __name__ == "__main__":
asyncio.run(
main(
sys.argv[1] if len(sys.argv) > 1 else ADDRESS,
sys.argv[2] if len(sys.argv) > 2 else CHARACTERISTIC_UUID,
)
)