-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsensor.py
40 lines (31 loc) · 1.4 KB
/
sensor.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
from blehrm import blehrm
from bleak import BleakScanner, BLEDevice
import asyncio
from typing import Dict
import logging
from PySide6.QtCore import QObject, Signal
class SensorHandler(QObject):
scan_complete = Signal()
def __init__(self):
super().__init__()
self.valid_devices: Dict[str, BLEDevice] = {}
self.logger = logging.getLogger(__name__)
def get_valid_device_names(self):
return list(self.valid_devices.keys())
async def scan(self):
'''
Scans for compatible BLE heart rate monitor devices
Emits the list of devices to devices_found
valid_devices is a list of strings, with the name of all valid sensors found
'''
self.logger.info('Scanning for devices...')
self.valid_devices = {}
while len(self.valid_devices) == 0: # Loop until supported device is found
ble_devices = await BleakScanner.discover()
supported_devices = blehrm.get_supported_devices(ble_devices)
self.valid_devices = {device.name: device for device, device_type in supported_devices}
self.logger.info(f"Found {len(ble_devices)} BLE devices, {len(self.valid_devices)} of which were valid")
await asyncio.sleep(1)
self.scan_complete.emit()
def create_sensor_client(self, device_name):
return blehrm.create_client(self.valid_devices[device_name])