我正在尝试检索外围设备的固件修订字符串。
通过应用程序询问我的外围设备" LightBlue"我能够查看设备信息,其中包括:
但是,在我的代码中,我无法发现固件修订字符串的特性。我尝试过以下UUID:
如何检索固件修订字符串?
答案 0 :(得分:1)
您需要首先发现相关的CBService。在这种情况下,它就是设备信息服务。
首先在类/结构中的某个位置定义设备信息服务的CBUUID和固件修订字符串
let deviceInformationServiceUUID = CBUUID(string: "180a")
let firmwareRevisionStringCharacteristicUUID = CBUUID(string: "2a26")
然后在您的CBCentralManagerDelegate中:
// 1. Discover the services we care about upon initial connection
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.discoverServices([deviceInformationServiceUUID])
}
// 2. Discover the characteristics of the services we care about
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard error == nil else {
print("Failed to discover services, error: \(error?.localizedDescription ?? "failed to obtain error description")")
return
}
if let services = peripheral.services {
services.forEach { peripheral.discoverCharacteristics(nil, for: $0) }
}
}
// 3. Interrogate the characteristics and single out the firmware revision string characteristic, and read its value
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard error == nil else {
print("Failed to discover characteristics for service \(service.uuid), error: \(error?.localizedDescription ?? "no error description")")
return
}
guard let discoveredCharacteristics = service.characteristics else {
print("peripheralDidDiscoverCharacteristics called for empty characteristics for service \(service.uuid)")
return
}
if service.uuid == deviceInformationServiceUUID {
for characteristic in discoveredCharacteristics {
if characteristic.uuid == firmwareRevisionStringCharacteristicUUID {
print("Reading FW revision string for peripheral...")
peripheral.readValue(for: characteristic)
break
}
}
}
}
// 4. Wait for value to be read and print it out
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard let data = characteristic.value else {
print("Unable to obtain notification/indication data from CBPeripheral")
return
}
if characteristic.uuid == firmwareRevisionStringCharacteristicUUID,
let firmwareRevisionString = String(data: data, encoding: .utf8) {
logger.log(message: "FW revision string read as \(firmwareRevisionString)!")
}
}