Skip to content

Commit

Permalink
End readValues(for:completionHandler:) when `setNotifyValue(false, …
Browse files Browse the repository at this point in the history
…for:)` is called
  • Loading branch information
exPHAT committed Sep 29, 2023
1 parent 91ff5fe commit 69af89d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Sources/SwiftBluetooth/Peripheral/Peripheral+callback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,33 @@ public extension Peripheral {
}

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

let shouldNotify = self.notifyingState.removeInternal(forKey: characteristic.uuid)

// We should only stop notifying when we have no internal handlers waiting on it
// and the last external `setNotifyValue` was set to false
//
// NOTE: External notifying tracking is currently disabled
self.cbPeripheral.setNotifyValue(shouldNotify, for: characteristic)
}

let eventSubscription = eventSubscriptions.queue { event, done in
guard case .updateNotificationState(let foundCharacteristic, _) = event,
foundCharacteristic.uuid == characteristic.uuid,
!foundCharacteristic.isNotifying else { return }

done()
} completion: {
valueSubscription.cancel()
}

notifyingState.addInternal(forKey: characteristic.uuid)
cbPeripheral.setNotifyValue(true, for: characteristic)

return subscription
return eventSubscription
}

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

0 comments on commit 69af89d

Please sign in to comment.