Replies: 1 comment
-
import asyncio
import logging
from bleak import BleakScanner, BleakClient
logger = logging.getLogger(__name__)
async def monitor(address: str):
while True:
logger.debug("scanning for device")
device = await BleakScanner.find_device_by_address(address)
if device is None:
logger.debug("no device found, wait then scan again")
await asyncio.sleep(30)
# TODO: may want to give up after X number of retries
continue
disconnect_event = asyncio.Event()
try:
logger.debug("connecting to device")
async with BleakClient(
device, disconnected_callback=lambda c: disconnect_event.set()
) as client:
logger.debug("connected to device")
await client.start_notify(...)
...
await disconnect_event.wait()
logger.debug("device disconnected")
except Exception:
logger.exception("exception while connecting/connected")
asyncio.run(monitor("00:00:00:00:00:00")) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I'm using Bleak to receive ADC measurements from a BC832 module that is mounted on a dynamic system. Sometimes, a connector will come transiently loose (for a few milliseconds), and this will stop my script from receiving new values from the module, even if the module is powered again within 1 s of disconnecting.
I'm looking for a way to incorporate code into my script so that, upon disconnecting, my computer will automatically scan for the same device UUID and attempt to re-connect. I'm not looking for anything fancy, but rather just a simple routine to incorporate this feature into my code.
Any ideas?
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions