如何读取特征值?
问题描述:
我目前正在使用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显示了read
,write
,notify
。但它确实获得了该特征的正确价值。
答
读取特征值。 swift 4
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for newChar: CBCharacteristic in service.characteristics!{
peripheral.readValue(for: newChar)
}
直到发出读取请求并获得对'didUpdateValue' CBPeripheralDelegate方法的回调之前,该值为零。 – Paulw11
当你发现它时,调用'func readValue(用于特征:CBCharacteristic)'。或setNotify(如果可用于字符)。 – Larme
它的工作原理。这些物业怎么样? – WeiJay