Willjay
Willjay

Reputation: 6459

How to read value of characteristics?

I'm currently working on BLE device with CoreBluetooth. I can find my device via CBCentralManagerDelegate and connect with my device.

When I want to discover the characteristics of a service, I can get the correct uuid, however, the value of characteristic is nil. Any ideas?

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    if error != nil {
        print("ERROR DISCOVERING CHARACTERISTICS: \(error?.localizedDescription)")
        return
    }
    if let characteristics = service.characteristics {

        for characteristic in characteristics {
            print("--------------------------------------------")
            print("Characteristic UUID: \(characteristic.uuid)")
            print("Characteristic isNotifying: \(characteristic.isNotifying)")
            print("Characteristic properties: \(characteristic.properties)")
            print("Characteristic descriptors: \(characteristic.descriptors)")
            print("Characteristic value: \(characteristic.value)")
        }
    }
}

-------------------------------------------------------------------
Characteristic UUID: FA01
Characteristic isNotifying: false
Characteristic properties: CBCharacteristicProperties(rawValue: 26)
Characteristic descriptors: nil
Characteristic value: nil

Another question about properties, according to Bluetooth SIG

enter image description here

Why nRFConnect shows up read, write, notify. But it indeed gets the right value of the characteristic.

enter image description here

Upvotes: 7

Views: 9021

Answers (1)

Deepak Tagadiya
Deepak Tagadiya

Reputation: 2237

read value of characteristics. swift 4

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
   for newChar: CBCharacteristic in service.characteristics!{

            peripheral.readValue(for: newChar)
        }

Upvotes: 4

Related Questions