Skip to content

Commit 69af89d

Browse files
committed
End readValues(for:completionHandler:) when setNotifyValue(false, for:) is called
1 parent 91ff5fe commit 69af89d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Sources/SwiftBluetooth/Peripheral/Peripheral+callback.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,33 @@ public extension Peripheral {
2727
}
2828

2929
func readValues(for characteristic: CBCharacteristic, onValueUpdate: @escaping (Data) -> Void) -> CancellableTask {
30-
let subscription = responseMap.queue(key: characteristic.uuid) { data, _ in
30+
let valueSubscription = responseMap.queue(key: characteristic.uuid) { data, _ in
3131
onValueUpdate(data)
3232
} completion: { [weak self] in
3333
guard let self = self else { return }
3434

3535
let shouldNotify = self.notifyingState.removeInternal(forKey: characteristic.uuid)
36-
3736
// We should only stop notifying when we have no internal handlers waiting on it
3837
// and the last external `setNotifyValue` was set to false
38+
//
39+
// NOTE: External notifying tracking is currently disabled
3940
self.cbPeripheral.setNotifyValue(shouldNotify, for: characteristic)
4041
}
4142

43+
let eventSubscription = eventSubscriptions.queue { event, done in
44+
guard case .updateNotificationState(let foundCharacteristic, _) = event,
45+
foundCharacteristic.uuid == characteristic.uuid,
46+
!foundCharacteristic.isNotifying else { return }
47+
48+
done()
49+
} completion: {
50+
valueSubscription.cancel()
51+
}
52+
4253
notifyingState.addInternal(forKey: characteristic.uuid)
4354
cbPeripheral.setNotifyValue(true, for: characteristic)
4455

45-
return subscription
56+
return eventSubscription
4657
}
4758

4859
func writeValue(_ data: Data, for characteristic: CBCharacteristic, type: CBCharacteristicWriteType, completionHandler: @escaping () -> Void) {

0 commit comments

Comments
 (0)