gpt4 book ai didi

swift - didDiscoverCharacteristicsFor 没有返回 HM-10 BLE 的任何特征

转载 作者:行者123 更新时间:2023-11-28 08:22:55 25 4
gpt4 key购买 nike

我成功地能够使用 didDiscoverServices 发现服务,从那里我调用以下函数来发现特征:

peripheral.discoverCharacteristics(nil, for: service as CBService)

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

if error != nil {
print("[ERROR] Error discovering characteristics. \(error!.localizedDescription)")
return
}

print("Found \(service.characteristics!.count) characteristics!: \(service.characteristics)")
}

这会打印:“找到 0 个特征!:可选([])”

我没有从 BLE 设备获得任何特征。感谢您的帮助。

最佳答案

一般在连接之后,下一步就是发现服务。现在每项服务都有自己的特点。在你上面打电话

peripheral.discoverCharacteristics(nil, for: service as CBService)

服务作为 CBService 可能有也可能没有任何特征。我建议你做一件事,检查你的 BLE 设备拥有的所有服务,然后发现所有服务的特征。

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
guard let serviceError = error else {
if let peripheralServices = peripheral.services {
for service in peripheralServices {
peripheral.discoverCharacteristics(nil, forService: service)
}
}
return
}

print("Error in discovering Services. Error : \(serviceError.localizedDescription)")
}

希望对你有帮助。快乐编码:)

关于swift - didDiscoverCharacteristicsFor 没有返回 HM-10 BLE 的任何特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40820666/

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