From 97334219bd0271fbe6d3de58ac2fcb23a6a3dd4b Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sun, 26 Mar 2023 11:24:02 -0500 Subject: [PATCH] pybricksdev.ble: filter name is None in find_device() A recent change in Bleak made this problem more apparent since it happens nearly all of the time on Windows now. It is possible to get an advertisement callback before the SCAN_RSP has been received in which case there will be no local name yet. So we need to ignore those responses and wait for one that does have the local name to ensure that the found device has a useful .name attribute. Fixes: https://github.com/orgs/pybricks/discussions/1010 --- CHANGELOG.md | 5 +++++ pybricksdev/ble/__init__.py | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 765bd06..3c68652 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Fixed `pybricks.ble.find_device()` returning with `name is None` on Windows ([support#1010]). + +[support#1010]: https://github.com/orgs/pybricks/discussions/1010 + ## [1.0.0-alpha.40] - 2023-03-22 ### Changed diff --git a/pybricksdev/ble/__init__.py b/pybricksdev/ble/__init__.py index 12819de..e09742e 100644 --- a/pybricksdev/ble/__init__.py +++ b/pybricksdev/ble/__init__.py @@ -27,7 +27,8 @@ async def find_device( name: The device name. This can also be the Bluetooth address on non-Apple platforms or a UUID on Apple platforms. If ``name`` is ``None`` then - it is not used as part of the matching criteria. + it is not used as part of the matching criteria. The name matching + is not case-sensitive. service: The service UUID that is advertized. timeout: @@ -45,11 +46,16 @@ def match_uuid_and_name(device: BLEDevice, adv: AdvertisementData): if service not in adv.service_uuids: return False + if adv.local_name is None: + # have not received SCAN_RSP yet + return False + if ( name is not None and adv.local_name != name and device.address.upper() != name.upper() ): + # filtering by name but name does not match return False return True