From 9403a9535b7559fa012162b37182765aa80b75b1 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Fri, 25 Nov 2022 17:30:57 -0600 Subject: [PATCH] bluezdbus/manager: suppress org.bluez.Error.NotReady when stopping scanner When stopping scanning, we don't want to raise errors if we can be sure that scanning is in fact stopped. `org.bluez.Error.NotReady` can be triggered if the Bluetooth adapter is turned off after scanning started, in which case we know that it is not scanning, so it is safe to ignore this error. --- CHANGELOG.rst | 3 ++- bleak/backends/bluezdbus/manager.py | 31 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cac65bc1..fb244031 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,7 +26,8 @@ Changed Fixed ----- -- Fixed ``AttributeError`` in ``_ensure_success`` in WinRT backend. +* Fixed ``AttributeError`` in ``_ensure_success`` in WinRT backend. +* Fixed ``BleakScanner.stop()`` can raise ``BleakDBusError`` with ``org.bluez.Error.NotReady`` in BlueZ backend. Fixed ----- diff --git a/bleak/backends/bluezdbus/manager.py b/bleak/backends/bluezdbus/manager.py index 8ee6e5d0..cfb826ed 100644 --- a/bleak/backends/bluezdbus/manager.py +++ b/bleak/backends/bluezdbus/manager.py @@ -27,7 +27,7 @@ from dbus_fast import BusType, Message, MessageType, Variant, unpack_variants from dbus_fast.aio.message_bus import MessageBus -from ...exc import BleakError +from ...exc import BleakDBusError, BleakError from ..service import BleakGATTServiceCollection from . import defs from .advertisement_monitor import AdvertisementMonitor, OrPatternLike @@ -385,20 +385,25 @@ async def stop() -> None: member="StopDiscovery", ) ) - assert_reply(reply) - # remove the filters - reply = await self._bus.call( - Message( - destination=defs.BLUEZ_SERVICE, - path=adapter_path, - interface=defs.ADAPTER_INTERFACE, - member="SetDiscoveryFilter", - signature="a{sv}", - body=[{}], + try: + assert_reply(reply) + except BleakDBusError as ex: + if ex.dbus_error != "org.bluez.Error.NotReady": + raise + else: + # remove the filters + reply = await self._bus.call( + Message( + destination=defs.BLUEZ_SERVICE, + path=adapter_path, + interface=defs.ADAPTER_INTERFACE, + member="SetDiscoveryFilter", + signature="a{sv}", + body=[{}], + ) ) - ) - assert_reply(reply) + assert_reply(reply) return stop except BaseException: