-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble_scan.py
74 lines (62 loc) · 2.38 KB
/
ble_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
# ruff: noqa: E402
sys.path.append("")
from micropython import const
import uasyncio as asyncio
import aioble
import bluetooth
import random
import struct
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
async def find_devices():
devices = {}
# Scan for 5 seconds, in active mode, with very low interval/window (to
# maximise detection rate).
async with aioble.scan(5000, interval_us=30000, window_us=30000, active=True) as scanner:
async for result in scanner:
print("Result: ",result)
print("Name: ",result.name(), "RSSI:",result.rssi)
if result.name() not in devices:
print("adding ", result.name(), result.device)
devices[result.name()] = result.device
print("Found: ",devices)
return devices
async def main():
devices = await find_devices()
for device in devices:
try:
print("Connecting to", device)
connection = await devices.get(device).connect()
print("Services: ", connection.services())
#await connection.services()._start()
except asyncio.TimeoutError:
print("Timeout during connection")
return
await asyncio.sleep_ms(1000)
serviceUUIDs = set()
async for service in connection.services():
print("Service: ",service)
serviceUUIDs.add(service.uuid)
# 0x1800 GAP Service
#0x2a01 BLE_UUID_GAP_CHARACTERISTIC_APPEARANCE
#0x2a00 BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME
#0x2a03 BLE_UUID_GAP_CHARACTERISTIC_RECONN_ADDR
#0x2a04 BLE_UUID_GAP_CHARACTERISTIC_PPCP
# 0x1801 GATT Service
#0x2a05 BLE_UUID_GATT_CHARACTERISTIC_SERVICE_CHANGED
# 0x180a Device Information Service
#0x2a26 BLE_UUID_FIRMWARE_REVISION_STRING_CHAR
#0x2a24 BLE_UUID_MODEL_NUMBER_STRING_CHAR
#0x2a29 BLE_UUID_MANUFACTURER_NAME_STRING_CHAR
#0x2a28 BLE_UUID_SOFTWARE_REVISION_STRING_CHAR
#0x2a27 BLE_UUID_HARDWARE_REVISION_STRING_CHAR
#0x2a50 BLE_UUID_PNP_ID_CHAR
# '98186d60-2f47-11e6-8899-0002a5d5c51b'
# uuid = bluetooth.UUID('98186d60-2f47-11e6-8899-0002a5d5c51b')
# service = await connection.service(uuid)
# print("Service:", service)
# async for char in service.characteristics():
# print(char)
asyncio.run(main())