gpt4 book ai didi

ios - swift 4 iOS : Does not discover services of connected BLE device

转载 作者:行者123 更新时间:2023-11-28 07:39:30 26 4
gpt4 key购买 nike

我正在尝试制作一个能够配置低功耗蓝牙设备的应用程序。现在应用程序已连接到设备,但据我所知,该应用程序不会发现 BLE 设备的服务。

我知道 BLE 设备有很多服务,因为已经有一个 Android 应用程序可以配置相同的设备。

我的连接代码如下:

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if peripheral.name == "Sensor"{
if let name = peripheral.name {
foundPeripherals.append(name)

sensor = peripheral
sensor.delegate = self
centralManager.connect(sensor, options: nil)

} else {
foundPeripherals.append(peripheral.identifier.uuidString)
}
RSSIs.append(RSSI)
if peripheral.name != nil {
// For viewing the data in the console
print("--->Peripheral device<---")
print("Name: \(peripheral.name as Any)")
print("UUID: \(peripheral.identifier.uuidString)")
print("RSSI: \(RSSI)")
print(" - - - - - - - - - - - - - - ")
print(advertisementData)
}
connectTableView.reloadData()

}
}


func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("-->Connected to Sensor<--")
print("Sensor info: \(peripheral)")
centralManager?.stopScan()

peripheral.delegate = self

peripheral.discoverServices(nil)

}

但是我在我的控制台中得到了这个输出:

--->Scanning started<---
--->Peripheral device<---
Name: Optional("Sensor")
UUID: 1AC3DC85-61E0-C3E9-0E33-D98F1D1CA791
RSSI: -43
- - - - - - - - - - - - - -
["kCBAdvDataServiceUUIDs": <__NSArrayM 0x2814852c0>(
6E400001-B5A3-F393-E0A9-E50E24DCCA9E
)
, "kCBAdvDataIsConnectable": 1, "kCBAdvDataLocalName": Sensor]
-->Connected to Sensor<--
Sensor info: <CBPeripheral: 0x282bd0820, identifier = 1AC3DC85-
61E0-C3E9-0E33-D98F1D1CA791, name = Sensor, state = connected>
2018-10-03 14:56:31.029079+0200 Sensor[9686:3266937]
[CoreBluetooth] API MISUSE: Discovering services for peripheral
<CBPeripheral: 0x282bd0820, identifier = 1AC3DC85-61E0-C3E9-0E33-
D98F1D1CA791, name = Sensor, state = connected> while delegate is
either nil or does not implement peripheral:didDiscoverServices:

它不发现/显示任何服务

希望它有意义并且你们能够提供帮助

-托马斯

最佳答案

在控制台中导入消息:

[CoreBluetooth] API MISUSE: Discovering services for peripheral < CBPeripheral: 0x282bd0820, identifier = 1AC3DC85-61E0-C3E9-0E33-D98F1D1CA791, name = Sensor, state = connected> while delegate is either nil or does not implement peripheral:didDiscoverServices:

您滥用了 CoreBluetooth 框架。

您编写的 peripheral.discoverServices(nil) 导致控制台中出现该错误。由于它不是 while delegate is either nil 这部分,因为你在 peripheral.delegate = self 之前写过,这意味着它是 while delegate does not implement 的部分外围设备:didDiscoverServices:

在 Swift 中,它是 peripheral(_:didDiscoverServices:),或者更明确地说是 optional func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)

这么说之后,您实现了它,执行了 peripheral.discoverCharacteristics(nil, for: service) 并得到了 while delegate is nil or does not implement peripheral:didDiscoverCharacteristicsForService:错误:

您看到与上一期的类比了吗?所以实现 peripheral(_:didDiscoverCharacteristicsFor:error:) 或更明确地:optional func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)

关于ios - swift 4 iOS : Does not discover services of connected BLE device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52627948/

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