Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions python_ble_proxy/matter_ble_proxy/bleak_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
if TYPE_CHECKING:
from collections.abc import Callable

from bleak.args.bluez import BlueZScannerArgs
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData as BleakAdvertisementData

Expand All @@ -34,8 +35,9 @@ class BleakScanSource(BleScanSource):
handshake) — typically tens to hundreds of milliseconds.
"""

def __init__(self) -> None:
def __init__(self, hci_device: int | None = None) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the flag is --hci-id and the argparse dest is hci_id, but this parameter is hci_device. Renaming it to hci_id would keep one name end-to-end (BleakScanSource(hci_id=args.hci_id)).

"""Initialize."""
self._hci_device = hci_device
self._scanner: BleakScanner | None = None
self._callback: Callable[[AdvertisementData], None] | None = None
# Cache the most recent BLEDevice per address so BleakDeviceResolver
Expand All @@ -53,7 +55,15 @@ async def start(self, callback: Callable[[AdvertisementData], None]) -> None:
self._callback = callback
if self._scanner is not None:
return
self._scanner = BleakScanner(detection_callback=self._on_detection)

bluez_args: BlueZScannerArgs = {}
if self._hci_device is not None:
bluez_args["adapter"] = f"hci{self._hci_device}"

self._scanner = BleakScanner(
detection_callback=self._on_detection,
bluez=bluez_args,
)
Comment thread
Apollon77 marked this conversation as resolved.
Outdated
await self._scanner.start()

async def stop(self) -> None:
Expand Down
12 changes: 9 additions & 3 deletions python_ble_proxy/matter_ble_proxy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def _parse_args(argv: list[str] | None) -> argparse.Namespace:
default="ws://localhost:5580/ble",
help="matter-server BLE proxy URL (default: %(default)s)",
)
parser.add_argument(
"--hci-id",
default=None,
type=int,
help="Bluetooth adapter HCI ID (e.g., 0 for hci0)",
)
Comment thread
Apollon77 marked this conversation as resolved.
Comment thread
Apollon77 marked this conversation as resolved.
parser.add_argument(
"--log-level",
default="INFO",
Expand All @@ -46,8 +52,8 @@ def _parse_args(argv: list[str] | None) -> argparse.Namespace:
return parser.parse_args(argv)


async def _run(server_url: str) -> int:
scan_source = BleakScanSource()
async def _run(server_url: str, hci_id: int | None) -> int:
scan_source = BleakScanSource(hci_id)
Comment thread
Apollon77 marked this conversation as resolved.
Outdated
device_resolver = BleakDeviceResolver(scan_source)
proxy = MatterBleProxy(server_url, scan_source, device_resolver)

Expand Down Expand Up @@ -96,7 +102,7 @@ def main(argv: list[str] | None = None) -> int:
datefmt="%H:%M:%S",
)
try:
return asyncio.run(_run(args.server))
return asyncio.run(_run(args.server, args.hci_id))
Comment on lines 104 to +105
except KeyboardInterrupt:
return 0

Expand Down
Loading