-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
64 lines (54 loc) · 2.25 KB
/
Copy pathreader.py
File metadata and controls
64 lines (54 loc) · 2.25 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
58
59
60
61
62
63
64
import sys
import asyncio
import pathlib
from bleak import BleakClient
from bleak.uuids import uuid16_dict
uuid16_dict = {v: k for k, v in uuid16_dict.items()}
filename = "ble_data.txt"
start_writing = False
USER_DATA_SERVICE_UUID = "0000{0:x}-0000-1000-8000-00805f9b34fb".format(
uuid16_dict.get("User Data") # 0x181C
)
STRING_CHARACTERISTIC_UUID = "0000{0:x}-0000-1000-8000-00805f9b34fb".format(
uuid16_dict.get("String") # 0x2A3D
)
last_index = -1
async def main(address: str):
while True:
print("Connecting to device...")
try:
async with BleakClient(address) as client:
global start_writing
start_writing = False
def coordinates_handler(_, data):
global start_writing
data_str = data.decode("utf-8")
if data_str == "stop":
start_writing = True
return
print(data_str)
if not start_writing:
return
pathlib.Path("out").mkdir(parents=True, exist_ok=True)
with open(f"out/{filename}", "a+") as f:
global last_index
if (data_str[0] != last_index):
f.write(data_str[1:] + "\n")
f.close()
last_index = data_str[0]
print(f"Connected: {client.is_connected}")
try:
while client.is_connected:
ids_service = (await client.get_services()).get_service(USER_DATA_SERVICE_UUID)
coordinates_char = ids_service.get_characteristic(STRING_CHARACTERISTIC_UUID)
coordinates_handler(None, await client.read_gatt_char(coordinates_char))
except Exception as e:
print(e)
except Exception as e:
print(e)
if __name__ == "__main__":
if len(sys.argv) == 2 or len(sys.argv) == 3:
filename = sys.argv[2] if len(sys.argv) == 3 else filename
asyncio.run(main(sys.argv[1]))
else:
print("Usage: pipenv run read <address> [<filename>]")