【问题标题】:How to read value of characteristics?如何读取特征值?
【发布时间】:2017-06-30 23:18:11
【问题描述】:

我目前正在使用 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

为什么nRFConnect 出现readwritenotify。但它确实得到了正确的特征值。

【问题讨论】:

  • 在您发出读取请求并获得对didUpdateValue CBPeripheralDelegate 方法的回调之前,该值将为零。
  • 发现时致电func readValue(for characteristic: CBCharacteristic)。或 setNotify(如果可用于 char)。
  • 它有效。属性怎么样?
  • 该特性允许读取、写入和通知。当您发现特征时,也会发现这些属性。接收到的原始值是 26,即 16 + 8 + 2,或者用十六进制代码编写:0x10 | 0x08 | 0x02,表示允许读取(0x02)、写入(0x08)和通知(0x10)。
  • 我不确定 readValueForCharacter vs setNotify。我正在从 LM35 读取有关“FFE1”特性的温度数据。在 setNotify 上为我工作但不是 readValueForCharacteristics....

标签: ios swift bluetooth-lowenergy core-bluetooth


【解决方案1】:

读取特征值。迅速 4

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

            peripheral.readValue(for: newChar)
        }

【讨论】:

  • 谢谢,这个答案帮了我很多忙!
  • 伟大的...@StijnSpijker
猜你喜欢
  • 2018-05-29
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
相关资源
最近更新 更多