Skip to content

Commit

Permalink
pybricksdev.ble: filter name is None in find_device()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dlech committed Mar 26, 2023
1 parent f298716 commit 9733421
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion pybricksdev/ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 9733421

Please sign in to comment.