CoreBluetooth Unable to Read Firmware Revision String

你离开我真会死。 提交于 2021-01-28 10:23:39

问题


I'm trying to retrieve my peripheral's Firmware Revision String.

When Interrogating my peripheral through the app "LightBlue" I'm able to view the Device Information which includes:

  • Manufacturer Name String
  • Firmware Revision String

However, in my code I'm unable to discover the characteristic for Firmware Revision String. I've tried the following UUID's:

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

How do I go about retrieving the Firmware Revision String?


回答1:


You need to first discover the relevant CBService. In this case it's the device information service.

First define the CBUUID for device information service and the firmware revision string somewhere in your class/struct

let deviceInformationServiceUUID = CBUUID(string: "180a")
let firmwareRevisionStringCharacteristicUUID = CBUUID(string: "2a26")

Then in your 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)!")
    }
}


来源:https://stackoverflow.com/questions/46433023/corebluetooth-unable-to-read-firmware-revision-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!