-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathnotify.py
More file actions
executable file
·57 lines (47 loc) · 1.43 KB
/
notify.py
File metadata and controls
executable file
·57 lines (47 loc) · 1.43 KB
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
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
from bleak import BleakClient, BleakScanner
import asyncio
import sys
import binascii
import time
u1 = "eb2eb8fd-ce11-44ac-babb-000000000001"
u2 = "eb2eb8fd-ce11-44ac-babb-000000000002"
def printstring(data):
s = ""
for b in data:
if b >= 32 and b <= 127:
s += chr(b)
else:
s += "."
print("HEX: " + ''.join(format(x, '02x') for x in data))
print("STRING: " + s)
def notify_callback(sender: int, data: bytearray):
print(data.decode('utf-8'))
#print(f"{sender}: {data}")
async def write(address):
ret = True
try:
async with BleakClient(address) as client:
print("connected!")
await client.start_notify(u1,notify_callback)
#await client.start_notify(u2,notify_callback)
await asyncio.sleep(1.0)
while True:
data = await client.read_gatt_char(u1)
printstring(data)
await asyncio.sleep(1.0)
#await client.stop_notify(u1)
#await client.stop_notify(u2)
except Exception as e:
print("PRINTING ERROR:")
print(e)
ret = False
return ret
if len(sys.argv) < 2:
print("usage: "+sys.argv[0]+" <device MAC> <characteristic UUID> <DATA>")
print("device MAC: MAC address of the BLE server")
sys.exit(1)
address = sys.argv[1]
ret = asyncio.run(write(address))
if not ret:
print("error")