From 5c4fa558767bf85eb4ba93e30bd982d9116bc539 Mon Sep 17 00:00:00 2001 From: Tilman Vogel Date: Mon, 26 Sep 2022 01:12:36 +0200 Subject: [PATCH 1/2] Fix RuntimeError about Future being attached to different loop This fixes an error about RuntimeError: Task cb=[_release_waiter(()]>)() at /usr/lib64/python3.8/asyncio/tasks.py:429]> got Future attached to a different loop in await asyncio.wait_for(self._notifyevent.wait(), timeout) at bleakconnection.py, line 101 --- eq3bt/bleakconnection.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/eq3bt/bleakconnection.py b/eq3bt/bleakconnection.py index d0d040d..56f8c65 100644 --- a/eq3bt/bleakconnection.py +++ b/eq3bt/bleakconnection.py @@ -31,14 +31,18 @@ def __init__(self, mac, iface): self._mac = mac self._iface = iface self._callbacks = {} - self._notifyevent = asyncio.Event() - self._notification_handle = None try: self._loop = asyncio.get_running_loop() except RuntimeError: self._loop = asyncio.new_event_loop() asyncio.set_event_loop(self._loop) + try: # necessary on python < 3.10 + self._notifyevent = asyncio.Event(loop=self._loop) + except TypeError: # raised on >= 3.10 + self._notifyevent = asyncio.Event() + + self._notification_handle = None def __enter__(self): """ From c062be4fbd3dc0c3395e6af6ffd467690810fa9e Mon Sep 17 00:00:00 2001 From: Tilman Vogel Date: Mon, 26 Sep 2022 01:15:20 +0200 Subject: [PATCH 2/2] Fix handle with Bleak 0.18.1 The callback now receives a BleakGATTCharacteristic object such that the integer handle needs to be retrieved from an attribute. --- eq3bt/bleakconnection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eq3bt/bleakconnection.py b/eq3bt/bleakconnection.py index 56f8c65..c588df1 100644 --- a/eq3bt/bleakconnection.py +++ b/eq3bt/bleakconnection.py @@ -83,10 +83,10 @@ def __exit__(self, exc_type, exc_val, exc_tb): self._loop.run_until_complete(self._conn.disconnect()) self._conn = None - async def on_notification(self, handle, data): + async def on_notification(self, characteristic, data): """Handle Callback from a Bluetooth (GATT) request.""" # The notification handles are off-by-one compared to gattlib and bluepy - handle = handle + 1 + handle = characteristic.handle + 1 _LOGGER.debug( "Got notification from %s: %s", handle, codecs.encode(data, "hex") )