forked from BOGUENSONG/BLE_debug_bleak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.py
58 lines (52 loc) · 2.19 KB
/
read_test.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
import asyncio
import sys
import argparse
from bleak import BleakScanner
from bleak import BleakClient
async def find_by_name(name: str):
global mac_addr
devices = await BleakScanner.discover()
print("found [%d] devices!" % len(devices))
for d in devices:
print("- %s (name: %s)" % (d.address, d.name))
if (d.name == name):
return d.address
return None
async def print_services(mac_addr: str, service_uuid: str, characteristic_uuid: str, loop: asyncio.AbstractEventLoop):
service_flag = 0
char_flag = 0
connect_flag = 0
try:
async with BleakClient(mac_addr, loop=loop, timeout=5.0) as client:
connect_flag = 1
svcs = client.services
for service in svcs:
if (service.uuid == service_uuid):
print("Service Found!")
service_flag = 1
for char in service.characteristics:
if (char.uuid == characteristic_uuid):
print("Characteristic Found! Read Start")
char_flag = 1
read = await client.read_gatt_char(characteristic_uuid)
print(read)
break
except:
print("not connected T.T please restart!!!!")
if ( (not service_flag) and connect_flag):
print("There is No Service : " + SERVICE_UUID)
if ( (not char_flag) and connect_flag):
print("There is No CHARACTERISTIC_UUID : " + CHARACTERISTIC_UUID)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('name', type=str, help='Device name')
parser.add_argument('service_uuid', type=str, help='Service UUID')
parser.add_argument('characteristic_uuid', type=str, help='Characteristic UUID')
args = parser.parse_args(sys.argv[1:])
loop = asyncio.get_event_loop()
mac_addr = loop.run_until_complete(find_by_name(args.name))
if (mac_addr is not None) :
print("Find! Connect start : " + mac_addr)
loop.run_until_complete(print_services(mac_addr, args.service_uuid, args.characteristic_uuid, loop))
else:
print("Sorry, There is No '%s'." % args.name)