'use events with bleak
I'm trying use the bleak python library over windows.
I want to scan for for a specific BLE device name and return it.
What I want to do is to start the scan and wait on an event till the callback is called (see the attached code).
Unfortunetally, the threading.wait does not work with event_loop.
Is there any other way to do this without busy wait?
What I want to do is something like the following:
@classmethod
def __detectionCallback(self, device, advertisement_data):
if device.name == self.__BleDeviceName:
self.__bleDevice = device
self.__deviceFoundEvent.set()
@classmethod
async def __getBleDevice_run(self, BleDeviceName: str, bleScanTimeout: int):
scanner = BleakScanner()
scanner.register_detection_callback(self.__detectionCallback)
self.__BleDeviceName = BleDeviceName
self.__deviceFoundEvent.clear()
await scanner.start()
# wait for event notifying a device is found
await self.__deviceFoundEvent.wait(bleScanTimeout)
await scanner.stop()
return self.__bleDevice
@classmethod
def getBleDevice(self, BleDeviceName: str, bleScanTimeout: int = 60):
loop = asyncio.get_event_loop()
return loop.run_until_complete(self.__getBleDevice_run(BleDeviceName, bleScanTimeout))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
