Skip to content

Commit 5700813

Browse files
committed
ble: allow finding device by service
This adds a parameter to allow finding a device that matches a service UUID in addition to a name. It also makes the name optional. This will help to ensure that we only connect to supported devices.
1 parent 386b136 commit 5700813

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

pybricksdev/ble/__init__.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@
33

44
import asyncio
55
import logging
6+
from typing import Optional
67

78
from bleak import BleakScanner, BleakClient
89
from bleak.backends.device import BLEDevice
910
from bleak.backends.scanner import AdvertisementData
1011

12+
from .pybricks import PYBRICKS_SERVICE_UUID
13+
1114
logger = logging.getLogger(__name__)
1215

1316

14-
async def find_device(name: str, timeout: float = 10) -> BLEDevice:
17+
async def find_device(
18+
name: Optional[str] = None,
19+
service: str = PYBRICKS_SERVICE_UUID,
20+
timeout: float = 10,
21+
) -> BLEDevice:
1522
"""Finds a BLE device that is currently advertising that matches the
1623
given parameters.
1724
1825
Arguments:
19-
name (str):
26+
name:
2027
The device name. This can also be the Bluetooth address on non-Apple
21-
platforms or a UUID on Apple platforms.
22-
timeout (float):
28+
platforms or a UUID on Apple platforms. If ``name`` is ``None`` then
29+
it is not used as part of the matching criteria.
30+
service:
31+
The service UUID that is advertized.
32+
timeout:
2333
How long to search before giving up.
2434
2535
Returns:
@@ -29,12 +39,18 @@ async def find_device(name: str, timeout: float = 10) -> BLEDevice:
2939
asyncio.TimeoutError:
3040
Device was not found within the timeout.
3141
"""
32-
print("Searching for {0}".format(name))
42+
print(f"Searching for {name or service}")
3343

3444
queue = asyncio.Queue()
3545

3646
def handle_detection(device: BLEDevice, adv: AdvertisementData):
37-
if adv.local_name != name and device.address.upper() != name.upper():
47+
if service not in adv.service_uuids:
48+
return
49+
if (
50+
name is not None
51+
and adv.local_name != name
52+
and device.address.upper() != name.upper()
53+
):
3854
return
3955
queue.put_nowait(device)
4056

pybricksdev/cli/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from argcomplete.completers import FilesCompleter
1717

1818
from .. import __name__ as MODULE_NAME, __version__ as MODULE_VERSION
19-
from ..ble.lwp3 import HubTypeId
19+
from ..ble.lwp3 import HubTypeId, LWP3_BOOTLOADER_SERVICE_UUID
2020

2121

2222
PROG_NAME = (
@@ -184,7 +184,7 @@ async def run(self, args: argparse.Namespace):
184184
from ..ble import find_device
185185
from ..flash import BootloaderConnection
186186

187-
device = await find_device("LEGO Bootloader")
187+
device = await find_device(service=LWP3_BOOTLOADER_SERVICE_UUID)
188188
print("Found:", device)
189189
updater = BootloaderConnection()
190190
await updater.connect(device)

0 commit comments

Comments
 (0)