Wait for writeWithoutResponse completion (#166)

* Wait for writeWithoutResponse request completion on Android

* Wait for writeWithoutResponse request completion on Darwin

* Add WriteWithoutResponse button in example app

* Update changelog and bump version

* Apply suggestions from code review

Co-authored-by: Foti Dim <foti@navideck.com>

---------

Co-authored-by: Foti Dim <foti@navideck.com>
This commit is contained in:
Rohit Sangwan
2025-06-26 11:35:59 +05:30
committed by GitHub
parent 0008231bdf
commit d56851cbfb
7 changed files with 60 additions and 34 deletions
+15 -3
View File
@@ -36,6 +36,7 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
private var discoveredServicesProgressMap: [String: [UniversalBleService]] = [:]
private var characteristicReadFutures = [CharacteristicReadFuture]()
private var characteristicWriteFutures = [CharacteristicWriteFuture]()
private var characteristicWriteWithoutResponseFutures = [CharacteristicWriteFuture]()
private var characteristicNotifyFutures = [CharacteristicNotifyFuture]()
private var discoverServicesFutures = [DiscoverServicesFuture]()
@@ -263,11 +264,12 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
}
peripheral.writeValue(value.data, for: gattCharacteristic, type: type)
// Wait for future response
let future = CharacteristicWriteFuture(deviceId: deviceId, characteristicId: gattCharacteristic.uuid.uuidStr, serviceId: gattCharacteristic.service?.uuid.uuidStr, result: completion)
if type == CBCharacteristicWriteType.withResponse {
// Wait for future response
characteristicWriteFutures.append(CharacteristicWriteFuture(deviceId: deviceId, characteristicId: gattCharacteristic.uuid.uuidStr, serviceId: gattCharacteristic.service?.uuid.uuidStr, result: completion))
characteristicWriteFutures.append(future)
} else {
completion(Result.success({}()))
characteristicWriteWithoutResponseFutures.append(future)
}
}
@@ -412,6 +414,16 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
}
}
public func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) {
characteristicWriteWithoutResponseFutures.removeAll { future in
if future.deviceId == peripheral.uuid.uuidString {
future.result(Result.success({}()))
return true
}
return false
}
}
public func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
characteristicWriteFutures.removeAll { future in
if future.deviceId == peripheral.uuid.uuidString && future.characteristicId == characteristic.uuid.uuidStr && future.serviceId == characteristic.service?.uuid.uuidStr {