CoreBluetooth无法读取固件修订字符串 [英] CoreBluetooth Unable to Read Firmware Revision String

查看:87
本文介绍了CoreBluetooth无法读取固件修订字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索外设的固件修订字符串.

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

通过"LightBlue"应用程序查询外围设备时,我可以查看设备信息,其中包括:

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

  • 制造商名称字符串
  • 固件修订字符串

但是,在我的代码中,我无法发现Firmware Revision String的特征.我已经尝试了以下UUID:

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?

推荐答案

您需要首先发现相关的CBService.在这种情况下,它就是设备信息服务.

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

首先在类/结构中的某个位置定义设备信息服务的CBUUID和固件修订字符串

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")

然后在您的CBCentralManagerDelegate中:

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

这篇关于CoreBluetooth无法读取固件修订字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆