0
votes

I am trying to make two programs that run on separate devices communicate with each other over bluetooth with CoreBluetooth. I can find and connect peripherals from the manager, and I can browse services in connected peripherals, but when I try and try and discover characteristics, I get the error The specified UUID is not allowed for this operation. and as expected the service's characteristics come up nil.

What is this supposed to mean? I have tried to discover characteristics with specifying the UUID of the target and without, both show this error.

this is the function that prints the error.

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    print(error.localizedDescription)//prints "The specified UUID is not allowed for this operation."
    if service.characteristics != nil {
        for characteristic in service.characteristics! {
            if characteristic.uuid == CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1") {
                print("characteristic found")
                peripheral.readValue(for: characteristic)
            }
        }
    }
}

this is where I look for peripherals.

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    if peripheral.services != nil {
        for service in peripheral.services! {
            if service.uuid == CBUUID(string: "dc495108-adce-4915-942d-bfc19cea923f") {
                peripheral.discoverCharacteristics(nil, for: service)
            }
        }
    }
}

this is how I add the service characteristic on the other device.

service = CBMutableService(type: CBUUID(string:"dc495108-adce-4915-942d-bfc19cea923f"), primary: true)
characteristic = CBMutableCharacteristic(type: CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1"), properties: .write, value: nil, permissions: .writeable)
service.characteristics = [characteristic]

I tried a number of different combinations of properties and permissions (including .read/.readable) and I get the same error.

1
You are trying to read a characteristic that you have set as write-only therefore you get the error when you try and read it.Paulw11
Makes sense, but how would I make it readable and writable? also I changed the property to .read and, and permissions to .readable and it doesn't change anything.C1FR1

1 Answers

1
votes

You are attempting to read the value of a characteristic that you have set as write-only, so Core Bluetooth gives you an error; the read operation is not valid for the specified characteristic.

If you want your characteristic to be both readable and writable you need to specify this:

service = CBMutableService(type: CBUUID(string:"dc495108-adce-4915-942d-bfc19cea923f"), primary: true)
let characteristic = CBMutableCharacteristic(type: CBUUID(string: "A4389A32-90D2-402F-A3DF-47996E123DC1"), properties: [.write, .read], value: nil, permissions: [.writeable, .readable])
service.characteristics = [characteristic]