-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
30 lines (24 loc) · 832 Bytes
/
main.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
import asyncio
from bleak import BleakScanner, BLEDevice
TARGET_DEVICE = "BlueToothAddress"
SAVE_TO_FILENAME = "Filename"
def callback(sender: BLEDevice, advertisement_data):
if sender.address == TARGET_DEVICE:
data_bytes = advertisement_data.manufacturer_data[343]
heart_rate = data_bytes[3]
print(f"Heart rate: {heart_rate} BPM")
with open(SAVE_TO_FILENAME,'w') as f:
f.write(str(heart_rate))
async def scan_for_devices():
scanner = BleakScanner(detection_callback=callback)
await scanner.start()
try:
while True:
await asyncio.sleep(1.0)
except KeyboardInterrupt:
pass
finally:
await scanner.stop()
print("Scanner stopped.")
loop = asyncio.get_event_loop()
loop.run_until_complete(scan_for_devices())