Skip to content

Commit

Permalink
Fix KeyError: 'delegate' in CoreBluetooth backend
Browse files Browse the repository at this point in the history
This fixes a KeyError due to a missing 'delegate' key in the metadata
dictionary in the BLEDevice returned in the advertisement callback.

This also adds other legacy metadata entries to the dictionary.

Fixes: hbldh#448
  • Loading branch information
dlech committed May 11, 2021
1 parent 48c44f4 commit 5113021
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
Fixed
~~~~~

* Fixed ``KeyError`` when trying to connect to ``BLEDevice`` from advertising
data callback on macOS. Fixes #448.
* Handling of undetected devices in ``connect_by_bledevice.py`` example. Fixes #487.
* Added ``Optional`` typehint for ``BleakScanner.find_device_by_address``.

Expand Down
19 changes: 15 additions & 4 deletions bleak/backends/corebluetooth/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,28 @@ def callback(p: CBPeripheral, a: Dict[str, Any], r: int):
manufacturer_value = bytes(manufacturer_binary_data[2:])
manufacturer_data[manufacturer_id] = manufacturer_value

service_uuids = [
cb_uuid_to_str(u) for u in a.get("kCBAdvDataServiceUUIDs", [])
]

advertisement_data = AdvertisementData(
local_name=p.name(),
manufacturer_data=manufacturer_data,
service_data=service_data,
service_uuids=[
cb_uuid_to_str(u) for u in a.get("kCBAdvDataServiceUUIDs", [])
],
service_uuids=service_uuids,
platform_data=(p, a, r),
)

device = BLEDevice(p.identifier().UUIDString(), p.name(), p, r)
device = BLEDevice(
p.identifier().UUIDString(),
p.name(),
p,
r,
uuids=service_uuids,
manufacturer_data=manufacturer_data,
service_data=service_data,
delegate=self._manager.central_manager.delegate(),
)

self._callback(device, advertisement_data)

Expand Down

0 comments on commit 5113021

Please sign in to comment.