From 7513ad29824e9aead7ab60eadaf92eca332e6173 Mon Sep 17 00:00:00 2001 From: wwj718 Date: Mon, 3 May 2021 12:41:40 +0800 Subject: [PATCH 1/3] fix ble bug and compatible with the use cases from the documentation --- pybricksdev/ble.py | 15 ++++++++------- pybricksdev/connections.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pybricksdev/ble.py b/pybricksdev/ble.py index 6c66ec1..f879332 100644 --- a/pybricksdev/ble.py +++ b/pybricksdev/ble.py @@ -9,16 +9,16 @@ from bleak.backends.scanner import AdvertisementData -async def find_device(name: str, timeout: float = 5) -> BLEDevice: - """Quickly find BLE device address by friendly device name. +async def find_device(device_or_address: str, timeout: float = 5) -> BLEDevice: + """Quickly find BLE device address by friendly device name or address. This is an alternative to bleak.discover. Instead of waiting a long time to scan everything, it returns as soon as it finds any device with the requested name. Arguments: - name (str): - Friendly device name. + device_or_address (str): + Friendly device name or address. timeout (float): When to give up searching. @@ -29,14 +29,15 @@ async def find_device(name: str, timeout: float = 5) -> BLEDevice: asyncio.TimeoutError: Device was not found within the timeout. """ - print("Searching for {0}".format(name)) + print("Searching for {0}".format(device_or_address)) queue = asyncio.Queue() def set_device_discovered(device: BLEDevice, _: AdvertisementData): - if device.name != name: + if device_or_address in [device.name, device.address]: + queue.put_nowait(device) + else: return - queue.put_nowait(device) async with BleakScanner(detection_callback=set_device_discovered): return await asyncio.wait_for(queue.get(), timeout=timeout) diff --git a/pybricksdev/connections.py b/pybricksdev/connections.py index 99428be..5a6dff5 100644 --- a/pybricksdev/connections.py +++ b/pybricksdev/connections.py @@ -723,7 +723,7 @@ async def connect(self, device: BLEDevice): RuntimeError: if Pybricks Protocol version is not supported """ self.logger.info(f"Connecting to {device.address}") - self.client = BleakClient(device) + self.client = BleakClient(device.address) def disconnected_handler(self, _: BleakClient): self.logger.info("Disconnected!") From 20fe5450617389ff1a2d52c5537284ee5da8544e Mon Sep 17 00:00:00 2001 From: wwj718 Date: Mon, 3 May 2021 12:58:33 +0800 Subject: [PATCH 2/3] fix ble bug --- pybricksdev/ble.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pybricksdev/ble.py b/pybricksdev/ble.py index f879332..af07066 100644 --- a/pybricksdev/ble.py +++ b/pybricksdev/ble.py @@ -103,7 +103,7 @@ async def connect(self, device: BLEDevice): """ print("Connecting to", device) - self.client = BleakClient(device) + self.client = BleakClient(device.address) await self.client.connect(disconnected_callback=self.disconnected_handler) await self.client.start_notify(self.char_tx_UUID, self.data_handler) print("Connected successfully!") From c1357c03c09349dc9081da9b33fe420c7ddc1b5c Mon Sep 17 00:00:00 2001 From: wwj718 Date: Mon, 3 May 2021 13:05:19 +0800 Subject: [PATCH 3/3] fix ble bug : runner.ipynb --- demo/runner.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/runner.ipynb b/demo/runner.ipynb index 1d68944..bfa05b3 100644 --- a/demo/runner.ipynb +++ b/demo/runner.ipynb @@ -33,8 +33,8 @@ " await ev3.connect('192.168.133.101')\n", " \n", " hub = BLEPUPConnection()\n", - " address = await find_device('Pybricks Hub', timeout=5)\n", - " await hub.connect(address)\n", + " device = await find_device('Pybricks Hub', timeout=5)\n", + " await hub.connect(device.address)\n", " \n" ] },