gpt4 book ai didi

ios - CoreBluetooth 无法读取固件修订字符串

转载 作者:行者123 更新时间:2023-11-29 11:41:17 26 4
gpt4 key购买 nike

我正在尝试检索外围设备的固件版本字符串。

当通过“LightBlue”应用程序询问我的外围设备时,我能够查看设备信息,其中包括:

  • 制造商名称字符串
  • 固件版本字符串

但是,在我的代码中,我无法发现固件修订字符串的特征。我尝试了以下 UUID:

  • 2A26
  • 0x2A26
  • 2a26
  • 0x2a26

如何检索固件修订字符串?

最佳答案

首先需要发现相关的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])
}

然后在CBPeripheralDelegate中:

// 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)!")
}
}

关于ios - CoreBluetooth 无法读取固件修订字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com