让NSOperation等待CoreBluetooth写入确认?

时间:2018-07-09 20:06:26

标签: ios swift core-bluetooth nsoperationqueue nsoperation

我希望使用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()功能?

0 个答案:

没有答案