Skip to content

Commit 9733421

Browse files
committed
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
1 parent f298716 commit 9733421

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Fixed
10+
- Fixed `pybricks.ble.find_device()` returning with `name is None` on Windows ([support#1010]).
11+
12+
[support#1010]: https://github.com/orgs/pybricks/discussions/1010
13+
914
## [1.0.0-alpha.40] - 2023-03-22
1015

1116
### Changed

pybricksdev/ble/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ async def find_device(
2727
name:
2828
The device name. This can also be the Bluetooth address on non-Apple
2929
platforms or a UUID on Apple platforms. If ``name`` is ``None`` then
30-
it is not used as part of the matching criteria.
30+
it is not used as part of the matching criteria. The name matching
31+
is not case-sensitive.
3132
service:
3233
The service UUID that is advertized.
3334
timeout:
@@ -45,11 +46,16 @@ def match_uuid_and_name(device: BLEDevice, adv: AdvertisementData):
4546
if service not in adv.service_uuids:
4647
return False
4748

49+
if adv.local_name is None:
50+
# have not received SCAN_RSP yet
51+
return False
52+
4853
if (
4954
name is not None
5055
and adv.local_name != name
5156
and device.address.upper() != name.upper()
5257
):
58+
# filtering by name but name does not match
5359
return False
5460

5561
return True

0 commit comments

Comments
 (0)