radkrish
radkrish

Reputation: 177

iOS: Writing is not permitted for write descriptor

I am building simple app 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))")
    }

Upvotes: 3

Views: 3152

Answers (1)

Paulw11
Paulw11

Reputation: 115012

Although CBPeripheral provides the writeValue(data:Data, descriptor: CBDescriptor) method and the documentation only states that you cannot write to descriptors of type CBUUIDClientCharacteristicConfigurationString, the CBPeripheralManagerDelegate does not offer any way of being notified about a write to a descriptor.

As a result you cannot write to any descriptor from a peripheral provided by CBPeripheralManager.

You should simply create another characteristic to write to.

Upvotes: 2

Related Questions