如何读取特征值?

时间:2017-02-13 03:16:09

标签: ios swift bluetooth-lowenergy core-bluetooth

我目前正在使用CoreBluetooth处理BLE设备。我可以通过CBCentralManagerDelegate找到我的设备并连接我的设备。

当我想要发现服务的特征时,我可以得到正确的uuid,但是,特征的值是nil。有什么想法吗?

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

有关属性的另一个问题,根据Bluetooth SIG

enter image description here

为什么nRFConnect会显示readwritenotify。但它确实得到了这个特征的正确价值。

enter image description here

1 个答案:

答案 0 :(得分:0)

读取特征值。 swift 4

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

            peripheral.readValue(for: newChar)
        }