ios ble“特色用户描述”

时间:2017-03-23 18:58:19

标签: ios swift bluetooth-lowenergy

尝试使用以下函数从特征中检索可读信息:

peripheral.discoverDescriptors(for: characteristic)

稍后委托方法:

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

被调用,但我怎样才能获得字符串描述?当我从描述符中读取值时,它总是nil

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

但是,如果我正在浏览并连接BLE扫描仪,它可以读取特有的人类可读描述符。

1 个答案:

答案 0 :(得分:0)

阅读描述符分为两个步骤,就像发现然后阅读特征一样。

尝试:

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

然后填写以下空白:

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
    }
}

字符串常量和关联的数据类型来自概览表here

在我(有限)的经历中,描述符并没有透露任何特别有趣的东西。