我正在尝试制作一个能够配置低功耗蓝牙设备的应用程序。到目前为止,该应用已连接到设备,但据我了解,该应用不会发现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:
它不会发现/显示任何服务
希望有道理,你们能够提供帮助
-托马斯
答案 0 :(得分:0)
在控制台中导入消息
[CoreBluetooth] API滥用:发现外围设备的服务< CBP外设:0x282bd0820,标识符= 1AC3DC85-61E0-C3E9-0E33-D98F1D1CA791,名称=传感器,状态= connect> ,而委托为nil或未实现 外围设备:didDiscoverServices:
您正在滥用CoreBluetooth框架。
您编写了peripheral.discoverServices(nil)
,导致控制台中出现该错误。
自从您在peripheral.delegate = self
之前编写以来,由于这不是部分,而委托不是n ,这意味着这是部分,而委托未实现peripheral:didDiscoverServices:
。
在Swift中,它是peripheral(_:didDiscoverServices:)
,或更明确地说是optional func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
说完之后,您实现了它,做了peripheral.discoverCharacteristics(nil, for: service)
并得到了,而委托不是nil或没有实现peripheral:didDiscoverCharacteristicsForService:error:
。
您看到与上一期的类比吗?因此,请实施peripheral(_:didDiscoverCharacteristicsFor:error:)
或更明确的方法:optional func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)