-
Notifications
You must be signed in to change notification settings - Fork 5
/
scan.py
59 lines (50 loc) · 2.01 KB
/
scan.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
56
57
58
59
"""
Service Explorer
----------------
An example showing how to access and print out the services, characteristics and
descriptors of a connected GATT server.
Created on 2019-03-25 by hbldh <[email protected]>
"""
import sys
import platform
import asyncio
import logging
from bleak import BleakClient
logger = logging.getLogger(__name__)
ADDRESS = (
"24:71:89:cc:09:05"
if platform.system() != "Darwin"
else "1FA49315-821B-5735-4F8D-4958D73E5AD5"
)
async def main(address):
async with BleakClient(address) as client:
logger.info(f"Connected: {client.is_connected}")
for service in client.services:
logger.info(f"[Service] {service}")
for char in service.characteristics:
if "read" in char.properties:
try:
value = bytes(await client.read_gatt_char(char.uuid))
logger.info(
f"\t[Characteristic] {char} ({','.join(char.properties)}), Value: {value}"
)
except Exception as e:
logger.error(
f"\t[Characteristic] {char} ({','.join(char.properties)}), Value: {e}"
)
else:
value = None
logger.info(
f"\t[Characteristic] {char} ({','.join(char.properties)}), Value: {value}"
)
for descriptor in char.descriptors:
try:
value = bytes(
await client.read_gatt_descriptor(descriptor.handle)
)
logger.info(f"\t\t[Descriptor] {descriptor}) | Value: {value}")
except Exception as e:
logger.error(f"\t\t[Descriptor] {descriptor}) | Value: {e}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(main(sys.argv[1] if len(sys.argv) == 2 else ADDRESS))