Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions demo/runner.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be device and not device.address?

" \n"
]
},
Expand Down
17 changes: 9 additions & 8 deletions pybricksdev/ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

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

We should also mention that "address" does not work on macOS. Instead, macOS assigns each device a UUID that has to be used instead of the Bluetooth 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.

Expand All @@ -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))
Copy link
Member

Choose a reason for hiding this comment

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

Since we need to make some other changes anyway, we could change this to:

Suggested change
print("Searching for {0}".format(device_or_address))
print(f"Searching for {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:
Copy link
Member

Choose a reason for hiding this comment

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

else: return is not needed since there is nothing in the function after this.

Copy link
Author

Choose a reason for hiding this comment

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

that is ok

return
queue.put_nowait(device)

async with BleakScanner(detection_callback=set_device_discovered):
return await asyncio.wait_for(queue.get(), timeout=timeout)
Expand Down Expand Up @@ -102,7 +103,7 @@ async def connect(self, device: BLEDevice):
"""

print("Connecting to", device)
self.client = BleakClient(device)
self.client = BleakClient(device.address)
Copy link
Member

Choose a reason for hiding this comment

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

By passing the address instead of device object will cause Bleak to have to scan for the device again. This would make connections much slower.

await self.client.connect(disconnected_callback=self.disconnected_handler)
await self.client.start_notify(self.char_tx_UUID, self.data_handler)
print("Connected successfully!")
Expand Down
2 changes: 1 addition & 1 deletion pybricksdev/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down