Skip to content

Commit

Permalink
CoreBluetooth: only notify if not read pending
Browse files Browse the repository at this point in the history
The same delegate callback is used for notifications and reading
characteristics. This rearranges the logic so that if there is a
read request pending, it will complete the read, otherwise it will
trigger a notification callback. Previously, reads would also trigger
notification callbacks.
  • Loading branch information
dlech committed Oct 23, 2021
1 parent 3f7dd22 commit 453c81b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions bleak/backends/corebluetooth/PeripheralDelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,17 @@ def did_update_value_for_characteristic(
):
c_handle = characteristic.handle()

if error is None:
notify_callback = self._characteristic_notify_callbacks.get(c_handle)
if notify_callback:
notify_callback(c_handle, bytearray(value))

future = self._characteristic_read_futures.get(c_handle)

# If there is no pending read request, then this must be a notification
# (the same delagate callback is used by both).
if not future:
return # only expected on read
if error is None:
notify_callback = self._characteristic_notify_callbacks.get(c_handle)
if notify_callback:
notify_callback(c_handle, bytearray(value))
return

if error is not None:
exception = BleakError(f"Failed to read characteristic {c_handle}: {error}")
future.set_exception(exception)
Expand Down

0 comments on commit 453c81b

Please sign in to comment.