Skip to content

Commit

Permalink
backends/bluezdbus/scanner: introduce bluez kwarg
Browse files Browse the repository at this point in the history
This adds a new bluez kwarg for backend-specific args and deprecates
the existing filters arg as per #623.
  • Loading branch information
dlech committed Jul 13, 2022
1 parent 257eb4f commit f22c076
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Added
-----

* Added new ``assigned_numbers`` module and ``AdvertisementDataType`` enum.
* Added new ``bluez`` kwarg to ``BleakScanner`` in BlueZ backend.

Changed
-------
Expand All @@ -24,6 +25,7 @@ Changed
* ``BleakScanner()`` args ``detection_callback`` and ``service_uuids`` are no longer keyword-only.
* ``BleakScanner()`` arg ``scanning_mode`` is no longer Windows-only and is no longer keyword-only.
* All ``BleakScanner()`` instances in BlueZ backend now use common D-Bus object manager.
* Deprecated ``filters`` kwarg in ``BleakScanner`` in BlueZ backend.


`0.14.3`_ (2022-04-29)
Expand Down
48 changes: 45 additions & 3 deletions bleak/backends/bluezdbus/scanner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import sys
from typing import Callable, Coroutine, Dict, List, Literal, Optional
from warnings import warn

from dbus_next import Variant

Expand All @@ -10,6 +12,32 @@

logger = logging.getLogger(__name__)

if sys.version_info >= (3, 8):
from typing import TypedDict

class BlueZDiscoveryFilters(TypedDict, total=False):
UUIDs: List[str]
RSSI: int
Pathloss: int
Transport: str
DuplicateData: bool
Discoverable: bool
Pattern: str

class BlueZArgs(TypedDict, total=False):
"""
:class:`BleakScanner` args that are specific to the BlueZ backend.
"""

filters: BlueZDiscoveryFilters
"""
Filters to pass to the adapter SetDiscoveryFilter D-Bus method.
"""

else:
BlueZDiscoveryFilters = Dict[str, object]
BlueZArgs = Dict[str, object]


class BleakScannerBlueZDBus(BaseBleakScanner):
"""The native Linux Bleak BLE Scanner.
Expand All @@ -28,17 +56,19 @@ class BleakScannerBlueZDBus(BaseBleakScanner):
also enables scanning while the screen is off on Android.
scanning_mode:
Set to ``"passive"`` to avoid the ``"active"`` scanning mode.
**bluez:
Dictionary of arguments specific to the BlueZ backend.
**adapter (str):
Bluetooth adapter to use for discovery.
**filters (dict):
A dict of filters to be applied on discovery.
"""

def __init__(
self,
detection_callback: Optional[AdvertisementDataCallback] = None,
service_uuids: Optional[List[str]] = None,
scanning_mode: Literal["active", "passive"] = "active",
*,
bluez: BlueZArgs = {},
**kwargs,
):
super(BleakScannerBlueZDBus, self).__init__(detection_callback, service_uuids)
Expand Down Expand Up @@ -66,7 +96,19 @@ def __init__(
if self._service_uuids:
self._filters["UUIDs"] = Variant("as", self._service_uuids)

self.set_scanning_filter(**kwargs)
filters = kwargs.get("filters")

if filters is None:
filters = bluez.get("filters")
else:
warn(
"the 'filters' kwarg is deprecated, use 'bluez' kwarg instead",
FutureWarning,
stacklevel=2,
)

if filters is not None:
self.set_scanning_filter(filters=filters)

async def start(self):
manager = await get_bluez_manager()
Expand Down

0 comments on commit f22c076

Please sign in to comment.