我希望使用NSOperation将CoreBluetooth写入队列:
class BtWriteOperation: Operation
{
private let data: Data
private let characteristic: CBCharacteristic
private let peripheral: CBPeripheral
private let completion: (Error?) -> ()
init(data: Data, characteristic: CBCharacteristic, completion: @escaping (Error?) -> ())
{
self.data = data
self.characteristic = characteristic
self.peripheral = characteristic.service.peripheral
self.completion = completion
super.init()
}
override final func main()
{
peripheral.writeValue(data, for: characteristic, type: .withResponse)
...let the operation wait...
let error = ...
completion(error)
}
}
另一个类将创建这些BtWriteOperation
,并将收到:
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?)
蓝牙传输完成后, CBPeripheralDelegate
通话。
收到此呼叫后,如何让BtWriteOperation
继续其main()
功能?