Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 645
Willjay Avatar asked Oct 18 '22 17:10

Willjay


1 Answers

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)
        }
like image 78
Deepak Tagadiya Avatar answered Oct 21 '22 04:10

Deepak Tagadiya