diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 378ac53..5faad6f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: hooks: - id: black -- repo: https://gitlab.com/pycqa/flake8 +- repo: https://github.com/pycqa/flake8 rev: 3.9.2 hooks: - id: flake8 diff --git a/eq3bt/bleakconnection.py b/eq3bt/bleakconnection.py index d0d040d..f573fa9 100644 --- a/eq3bt/bleakconnection.py +++ b/eq3bt/bleakconnection.py @@ -31,15 +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() + _LOGGER.debug("Using existing asyncio loop") except RuntimeError: self._loop = asyncio.new_event_loop() + _LOGGER.debug("Creating new asyncio loop") asyncio.set_event_loop(self._loop) + self._notifyevent = asyncio.Event() + def __enter__(self): """ Context manager __enter__ for connecting the device @@ -79,10 +82,14 @@ 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 + try: + handle = characteristic.handle + 1 + except: # noqa: E722 # fallback to old-style, int-based handle + handle = characteristic + 1 + _LOGGER.debug( "Got notification from %s: %s", handle, codecs.encode(data, "hex") )