Skip to content

Commit 5b74fab

Browse files
committed
Add BleakNoPassiveScanError exception
This exception can be used to e.g. retry scanning with scan_mode adjusted to "active" on systems not supporting the "passive" scan.
1 parent 35a0784 commit 5b74fab

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Added
1515
* Added optional hack to use Bluetooth address instead of UUID on macOS.
1616
* Added ``BleakScanner.find_device_by_name()`` class method.
1717
* Added check to verify that BlueZ Advertisement Monitor was actually registered
18+
* Added ``BleakNoPassiveScanError`` exception
1819

1920
Changed
2021
-------

bleak/backends/bluezdbus/manager.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from dbus_fast import BusType, Message, MessageType, Variant, unpack_variants
2828
from dbus_fast.aio.message_bus import MessageBus
2929

30-
from ...exc import BleakError
30+
from ...exc import BleakError, BleakNoPassiveScanError
3131
from ..service import BleakGATTServiceCollection
3232
from . import defs
3333
from .advertisement_monitor import AdvertisementMonitor, OrPatternLike
@@ -465,8 +465,9 @@ async def passive_scan(
465465
reply.message_type == MessageType.ERROR
466466
and reply.error_name == "org.freedesktop.DBus.Error.UnknownMethod"
467467
):
468-
raise BleakError(
469-
"passive scanning on Linux requires BlueZ >= 5.55 with --experimental enabled and Linux kernel >= 5.10"
468+
raise BleakNoPassiveScanError(
469+
"passive scanning on Linux requires BlueZ >= 5.55 with --experimental enabled"
470+
" and Linux kernel >= 5.10"
470471
)
471472

472473
assert_reply(reply)
@@ -507,7 +508,7 @@ async def passive_scan(
507508

508509
# Monitor might not be Activate-d yet, but for sure it shall not be Release-d
509510
if monitor.active is False:
510-
raise BleakError(
511+
raise BleakNoPassiveScanError(
511512
"Advertising Monitor (required for passive scanning) is not supported by this kernel"
512513
" (Linux kernel >= 5.10 is required)"
513514
)

bleak/backends/corebluetooth/scanner.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from CoreBluetooth import CBPeripheral
1212
from Foundation import NSBundle
1313

14-
from ...exc import BleakError
14+
from ...exc import BleakNoPassiveScanError
1515
from ..scanner import AdvertisementData, AdvertisementDataCallback, BaseBleakScanner
1616
from .CentralManagerDelegate import CentralManagerDelegate
1717
from .utils import cb_uuid_to_str
@@ -77,7 +77,7 @@ def __init__(
7777
self._use_bdaddr = cb.get("use_bdaddr", False)
7878

7979
if scanning_mode == "passive":
80-
raise BleakError("macOS does not support passive scanning")
80+
raise BleakNoPassiveScanError("macOS does not support passive scanning")
8181

8282
self._manager = CentralManagerDelegate.alloc().init()
8383
self._timeout: float = kwargs.get("timeout", 5.0)
@@ -89,7 +89,8 @@ def __init__(
8989
# See https://github.com/hbldh/bleak/issues/720
9090
if NSBundle.mainBundle().bundleIdentifier() == "org.python.python":
9191
logger.error(
92-
"macOS 12.0, 12.1 and 12.2 require non-empty service_uuids kwarg, otherwise no advertisement data will be received"
92+
"macOS 12.0, 12.1 and 12.2 require non-empty service_uuids kwarg,"
93+
" otherwise no advertisement data will be received"
9394
)
9495

9596
async def start(self):

bleak/exc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ def __str__(self) -> str:
6363
return (name + " " + details) if details else name
6464

6565

66+
class BleakNoPassiveScanError(BleakError):
67+
"""
68+
Exception raised when passive scan is started on the system not supporting it
69+
"""
70+
71+
pass
72+
73+
6674
CONTROLLER_ERROR_CODES = {
6775
0x00: "Success",
6876
0x01: "Unknown HCI Command",

0 commit comments

Comments
 (0)