简体   繁体   English

ios ble“典型的用户描述”

[英]ios ble “Characteristic User Description”

Trying to retrieve readable information from an characteristics by using the function: 通过使用以下函数尝试从特征中检索可读信息:

peripheral.discoverDescriptors(for: characteristic)

Later the delegate method: 后来的委托方法:

func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) 

is called but how can I get the string description? 被称为,但如何获得字符串描述? When I read the value from the descriptors it's always nil . 当我从描述符中读取值时,它始终为nil

let descriptors = characteristic.descriptors! as [CBDescriptor]
for descriptor in descriptors {
    print("\(#function): descriptor = \(descriptor) UUID = \(descriptor.uuid) value = \(descriptor.value)")
}

However, if I'm browsing and connecting with an BLE scanner it can read the characteristic human readable descriptors. 但是,如果我正在浏览并连接BLE扫描仪,它可以读取人类可读的特征描述符。

Reading descriptors is a two-step process, much like discovering and then reading characteristics. 读取描述符是一个两步过程,非常类似于发现然后读取特征。

Try: 尝试:

public func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
    guard let descriptors = characteristic.descriptors else { return }

    for descr in descriptors {
        peripheral.readValue(for: descr)
    }
}

And then fill in the blanks of: 然后填写以下内容:

public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {
    switch descriptor.uuid.uuidString {
    case CBUUIDCharacteristicExtendedPropertiesString:
        guard let properties = descriptor.value as? NSNumber else {
            break
        }
        print("  Extended properties: \(properties)")
    case CBUUIDCharacteristicUserDescriptionString:
        guard let description = descriptor.value as? NSString else {
            break
        }
        print("  User description: \(description)")
    case CBUUIDClientCharacteristicConfigurationString:
        guard let clientConfig = descriptor.value as? NSNumber else {
            break
        }
        print("  Client configuration: \(clientConfig)")
    case CBUUIDServerCharacteristicConfigurationString:
        guard let serverConfig = descriptor.value as? NSNumber else {
            break
        }
        print("  Server configuration: \(serverConfig)")
    case CBUUIDCharacteristicFormatString:
        guard let format = descriptor.value as? NSData else {
            break
        }
        print("  Format: \(format)")
    case CBUUIDCharacteristicAggregateFormatString:
        print("  Aggregate Format: (is not documented)")
    default:
        break
    }
}

The string constants and the associated data types came from the overview table here . 字符串常量和关联的数据类型来自此处的概览表。

In my (limited) experience, descriptors don't reveal anything particularly interesting. 以我的(有限的)经验,描述符没有揭示出任何特别有趣的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM