From 3f11e5d9954d2a7a58220d7462da6355b373fad7 Mon Sep 17 00:00:00 2001 From: Mark O'Hara Date: Sat, 13 Feb 2021 15:01:55 +0000 Subject: [PATCH] Add peripheralIsReady support for writes without response --- Bluejay/Bluejay/Event.swift | 1 + Bluejay/Bluejay/Peripheral.swift | 5 +++++ Bluejay/Bluejay/WriteCharacteristic.swift | 15 ++++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Bluejay/Bluejay/Event.swift b/Bluejay/Bluejay/Event.swift index 394d19a..c98b542 100644 --- a/Bluejay/Bluejay/Event.swift +++ b/Bluejay/Bluejay/Event.swift @@ -18,5 +18,6 @@ enum Event { case didDisconnectPeripheral(Peripheral) case didReadCharacteristic(CBCharacteristic, Data) case didWriteCharacteristic(CBCharacteristic) + case isReadyToWriteWithoutResponse case didUpdateCharacteristicNotificationState(CBCharacteristic) } diff --git a/Bluejay/Bluejay/Peripheral.swift b/Bluejay/Bluejay/Peripheral.swift index 5a93dbe..f7184d2 100644 --- a/Bluejay/Bluejay/Peripheral.swift +++ b/Bluejay/Bluejay/Peripheral.swift @@ -295,6 +295,11 @@ extension Peripheral: CBPeripheralDelegate { public func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { handle(event: .didWriteCharacteristic(characteristic), error: error as NSError?) } + + /// Captures CoreBluetooth's peripheral is ready to send write without response event and pass it to Bluejay's queue for processing. + func peripheralIsReady(toSendWriteWithoutResponse peripheral: CBPeripheral) { + handle(event: .isReadyToWriteWithoutResponse, error: nil) + } /// Captures CoreBluetooth's did receive a notification/value from a characteristic event and pass it to Bluejay's queue for processing. public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { diff --git a/Bluejay/Bluejay/WriteCharacteristic.swift b/Bluejay/Bluejay/WriteCharacteristic.swift index bae77da..a8a4898 100644 --- a/Bluejay/Bluejay/WriteCharacteristic.swift +++ b/Bluejay/Bluejay/WriteCharacteristic.swift @@ -57,14 +57,19 @@ class WriteCharacteristic: Operation { peripheral.writeValue(value.toBluetoothData(), for: characteristic, type: type) debugLog("Started write to \(characteristicIdentifier.description) on \(peripheral.identifier).") - - if type == .withoutResponse { - process(event: .didWriteCharacteristic(characteristic)) - } } func process(event: Event) { - if case .didWriteCharacteristic(let wroteTo) = event { + if case .isReadyToWriteWithoutResponse = event { + state = .completed + + debugLog("Write to \(characteristicIdentifier.description) on \(peripheral.identifier) is successful.") + + callback?(.success) + callback = nil + + updateQueue() + } else if case .didWriteCharacteristic(let wroteTo) = event { if wroteTo.uuid != characteristicIdentifier.uuid { preconditionFailure("Expecting write to \(characteristicIdentifier.description), but actually wrote to \(wroteTo.uuid)") }