0

I have the following code in the peripheral manager delegate

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
    print ("received write request")
    
    for request in requests {
        if request.characteristic.uuid.isEqual(self.primaChar.uuid) {
            // extract data from write request
            // TODO: validate data
            let str = String(data: request.value!, encoding: .utf8)!
            print ("received:", str)

            peripheralManager.respond(to: request, withResult: .success)
            
        } else {
            print ("Unknown write request")
        }
    }
}

After the response call

peripheralManager.respond(to: request, withResult: .success)

didWrite isn't called at the central side

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
    #if DEBUG
    CLog.debug ("Did write value for \(descriptor.characteristic)")
    #endif
}

I need to initiate multiple write requests but need to know if the previous write was successful, before sending the next write request.

Both the devices are paired.

Why is central manager's didWriteValueFor() not getting called?

greenhorn
  • 1,097
  • 6
  • 10
  • 1
    That looks like the wrong delegate method. You want [`didWriteValueForCharscteristic`](https://developer.apple.com/documentation/corebluetooth/cbperipheraldelegate/1518823-peripheral) not for descriptor. Also make sure you are issuing a write with response – Paulw11 Feb 26 '21 at 09:03
  • @Paulw11 Thanks, that fixed the problem. I was using the wrong callback. What do you mean by 'issuing a write' with response? – greenhorn Feb 26 '21 at 18:44
  • 1
    There are two possible writes with BLE. Write with response and write without response. In the latter case you don't get any feedback that the write is complete. – Paulw11 Feb 26 '21 at 19:22
  • Oh, ok. Got it! Thanks ! – greenhorn Feb 26 '21 at 21:45

1 Answers1

0

Using didWriteValueForCharscteristic() fixed the problem as suggested by @Paulw11 in the comments. Thanks Paul.

greenhorn
  • 1,097
  • 6
  • 10