2

I wish to queue CoreBluetooth writes using NSOperation:

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)
    }
}

Another class will create these BtWriteOperations and will receive the:

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?)

CBPeripheralDelegate call after the Bluetooth transfer is completed.

On reception of this call how do I let the BtWriteOperation continue its main() function?

meaning-matters
  • 21,929
  • 10
  • 82
  • 142

0 Answers0