-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetoothlowenergy.py
55 lines (47 loc) · 1.81 KB
/
bluetoothlowenergy.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
49
50
51
52
53
54
55
import platform
import asyncio
import logging
from bleak import BleakClient
async def run(address, loop, debug=False):
log = logging.getLogger(__name__)
if debug:
import sys
loop.set_debug(True)
log.setLevel(logging.DEBUG)
h = logging.StreamHandler(sys.stdout)
h.setLevel(logging.DEBUG)
log.addHandler(h)
async with BleakClient(address, loop=loop) as client:
x = await client.is_connected()
log.info("Connected: {0}".format(x))
for service in client.services:
log.info("[Service] {0}: {1}".format(service.uuid, service.description))
for char in service.characteristics:
if "read" in char.properties:
try:
value = bytes(await client.read_gatt_char(char.uuid))
except Exception as e:
value = str(e).encode()
else:
value = None
log.info(
"\t[Characteristic] {0}: ({1}) | Name: {2}, Value: {3} ".format(
char.uuid, ",".join(char.properties), char.description, value
)
)
for descriptor in char.descriptors:
value = await client.read_gatt_descriptor(descriptor.handle)
log.info(
"\t\t[Descriptor] {0}: (Handle: {1}) | Value: {2} ".format(
descriptor.uuid, descriptor.handle, bytes(value)
)
)
if __name__ == "__main__":
address = (
"F5:D2:A7:12:62:5A"
if platform.system() != "Darwin"
else "F5:D2:A7:12:62:5A"
)
loop = asyncio.get_event_loop()
print(address)
loop.run_until_complete(run(address, loop, True))