-
Notifications
You must be signed in to change notification settings - Fork 22
fix ble bug and compatible with the use cases from the documentation #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
|
|
||||||
|
|
@@ -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)) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
|
|
||||||
| 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: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
|
|
@@ -102,7 +103,7 @@ async def connect(self, device: BLEDevice): | |||||
| """ | ||||||
|
|
||||||
| print("Connecting to", device) | ||||||
| self.client = BleakClient(device) | ||||||
| self.client = BleakClient(device.address) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!") | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
deviceand notdevice.address?