Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Bluejay/Bluejay/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ enum Event {
case didDisconnectPeripheral(Peripheral)
case didReadCharacteristic(CBCharacteristic, Data)
case didWriteCharacteristic(CBCharacteristic)
case isReadyToWriteWithoutResponse
case didUpdateCharacteristicNotificationState(CBCharacteristic)
}
5 changes: 5 additions & 0 deletions Bluejay/Bluejay/Peripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
15 changes: 10 additions & 5 deletions Bluejay/Bluejay/WriteCharacteristic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ class WriteCharacteristic<T: Sendable>: 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)")
}
Expand Down