This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I am attempting to write a characteristic (the LED) on the Thingy52 and I get: "Optional(Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.}).

I'm confused by this because I believe I am writing to the correct LED Characteristic.  The code looks like:

...

         if characteristic.properties.contains(.write) {

            if (characteristic.uuid.uuidString == "EF680301-9B35-4933-9B10-52FFA9740042") {

                print("peripheral = \(peripheral)")

                print("UUID \(characteristic.uuid)- properties contains .write")

                print("write new settings to \(characteristic)")

                writeCharacteristic( peripheral: peripheral, withCharacteristic: characteristic, withValue: Data([0x01]))

                }

}

The writeCharacteristic code looks like:

    func writeCharacteristic( peripheral: CBPeripheral, withCharacteristic characteristic: CBCharacteristic, withValue value: Data) {

        print("writeCharacteristic, peripheral = \(peripheral)")

        print("writeCharacteristic, characteristic = \(characteristic)")

        peripheral.writeValue(value, for: characteristic, type: .withResponse)

    }

When I run this, it reports:

peripheral = <CBPeripheral: 0x2803b4000, identifier = 1B95385C-92A1-EC75-476F-6C3AFBCB511A, name = Thingy, state = connected>

UUID EF680301-9B35-4933-9B10-52FFA9740042- properties contains .write

write new settings to <CBCharacteristic: 0x2816b4600, UUID = EF680301-9B35-4933-9B10-52FFA9740042, properties = 0xA, value = (null), notifying = NO>

writeCharacteristic, peripheral = <CBPeripheral: 0x2803b4000, identifier = 1B95385C-92A1-EC75-476F-6C3AFBCB511A, name = Thingy, state = connected>

writeCharacteristic, characteristic = <CBCharacteristic: 0x2816b4600, UUID = EF680301-9B35-4933-9B10-52FFA9740042, properties = 0xA, value = (null), notifying = NO>

When 'didWriteValueFor' is called, it reports:

Error for: <CBPeripheral: 0x2803b4000, identifier = 1B95385C-92A1-EC75-476F-6C3AFBCB511A, name = Thingy, state = connected>

characteristic: <CBCharacteristic: 0x2816b4600, UUID = EF680301-9B35-4933-9B10-52FFA9740042, properties = 0xA, value = {length = 5, bytes = 0x020214ac0d}, notifying = NO>

Error discovering services: Optional(Error Domain=CBATTErrorDomain Code=3 "Writing is not permitted." UserInfo={NSLocalizedDescription=Writing is not permitted.})

 

and the LED isn't updated (I am just trying to set it to a solid color, not breathing, writing  0x01) I assume I am doing something wrong regarding the characteristic I am using but can't figure it out, any assistance someone could provide would be very much appreciated!

Parents Reply
  • Thanks, I didn't realize that.  I updated the code to read:

      if characteristic.properties.contains(.write) {

                if (characteristic.uuid.uuidString == "EF680301-9B35-4933-9B10-52FFA9740042") {

                    print("peripheral = \(peripheral)")

                    print("UUID \(characteristic.uuid)- properties contains .write")

                    let onArray:[UInt8] = [ 0x01, 0xff, 0xff, 0xff ]

                    let ofArray:[UInt8] = [ 0x00 ]

                    let onData:Data = Data(onArray)

                    let ofData:Data = Data(ofArray)

                    //peripheral.writeValue(onData, for: characteristic, type: .withResponse)

                    peripheral.writeValue(ofData, for: characteristic, type: .withResponse)

                }

    and it is now working.  Apparently the number of bytes sent is dependent on the mode byte (one byte for off, 4 bytes for on, 4 for breath, 2 for one-shot).

Children
No Data
Related