简体   繁体   中英

CoreBluetooth peripheral response to write request

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM