Tuesday 17 July 2018

iOS: Writing is not permitted for write descriptor

I am building simple for Central and Peripheral sample,
Central code works fine as I am able to write both characteristics and descriptor when Android acts as Peripheral,
But when iOS acts as Peripheral,I am unable to write descriptor,
I getting as Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.}
I believe,I had missed something,Any idea what am I doing wrong?
//Peripheral Code
           transferCharacteristic = CBMutableCharacteristic(
                type: transferCharacteristicUUID,
                properties: [.indicate,.writeWithoutResponse,.read],
                value: nil,
                permissions: [.writeable,.readable]
            )

            let userDesc = CBMutableDescriptor(type: userCharacteristicUUID, value: "Used to send commands")

            transferCharacteristic?.descriptors = [userDesc]

            // Then the service
            transferService = CBMutableService(
                type: transferServiceUUID,
                primary: true
            )

            // Add the characteristic to the service
            transferService?.characteristics = [transferCharacteristic!]
            peripheralManager!.add(transferService!)
            peripheralManager!.startAdvertising([CBAdvertisementDataServiceUUIDsKey : 
[transferServiceUUID],CBAdvertisementDataLocalNameKey : "Broadcaster"])


//Central Code where it throw error
 /** This callback lets us know more data has arrived via notification on the 
characteristic
     */
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic:
 CBCharacteristic, error: Error?) {
    var clientDes : CBDescriptor?

    for desc in characteristic.descriptors!{
        if desc.uuid.uuidString  == CBUUIDCharacteristicUserDescriptionString {
            clientDes = desc
        }
    }
 let sendValue:[UInt8] = [0x02, 0x00]
        let data = NSData(bytes: sentValue, length: 2)
        peripheral.writeValue(data as Data, for: clientDes!)
}

//Here I am getting the log
    func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor:
 CBDescriptor, error: Error?) {
        print("wrote to \(String(describing: error))")
    }



from iOS: Writing is not permitted for write descriptor

No comments:

Post a Comment