-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_hr.py
68 lines (51 loc) · 1.74 KB
/
bluetooth_hr.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
import time
import csv
from bleak import BleakClient
import asyncio
from bleak import BleakScanner
# discover devices
async def discover():
devices = await BleakScanner.discover()
num_devices = 0
for d in devices:
if "fenix" in d.__str__():
print(d)
else:
num_devices += 1
print(f"Found {num_devices} devices")
asyncio.run(discover())
address = "B4:C2:6A:A9:A4:04"
HRM_UUID = "00002a37-0000-1000-8000-00805f9b34fb"
# connect to device
async def main(address):
async with BleakClient(address) as client:
model_number = await client.read_gatt_char(HRM_UUID)
print("Model Number: {0}".format("".join(map(chr, model_number))))
asyncio.run(main(address))
# connect and read data
async def connect_and_read(address):
async with BleakClient(address) as client:
if await client.is_connected():
print("Connected to device")
await client.start_notify(HRM_UUID, callback_handler)
async def callback_handler(sender, data):
# Process the heart rate data received
print("Heart Rate:", int.from_bytes(data, byteorder='little'))
loop = asyncio.get_event_loop()
loop.run_until_complete(connect_and_read(address))
# write data to file
heart_rates = []
# def callback_handler(sender, data):
# heart_rate = int.from_bytes(data, byteorder='little')
# heart_rates.append(heart_rate)
# print("Heart Rate:", heart_rate)
# Run for 1 minute
start_time = time.time()
while time.time() - start_time < 60:
loop.run_until_complete(connect_and_read(address))
# Save to CSV
with open('heart_rate_data.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Heart Rate"])
for rate in heart_rates:
writer.writerow([rate])