[英]Swift 4 iOS: Does not discover services of connected BLE device
我正在嘗試制作一個能夠配置低功耗藍牙設備的應用程序。 到目前為止,該應用程序已連接到該設備,但據我了解,該應用程序不會發現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誤用 :發現外圍設備服務<CBP外圍設備:0x282bd0820,標識符= 1AC3DC85-61E0-C3E9-0E33-D98F1D1CA791,名稱=傳感器,狀態=已連接>, 而委托為nil或未實現外圍設備:didDiscoverServices:
您正在濫用CoreBluetooth框架。
您編寫了peripheral.discoverServices(nil)
,這導致控制台中出現該錯誤。 由於同時委托是因為你以前寫要么無它不是一部分peripheral.delegate = self
,這意味着它的一部分,同時委托未實現peripheral:didDiscoverServices:
在Swift中,它是peripheral(_:didDiscoverServices:)
或更明確的optional func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
說完之后,您實現了它, 執行了peripheral:didDiscoverCharacteristicsForService:error:
peripheral.discoverCharacteristics(nil, for: service)
並在委托為零或未實現peripheral:didDiscoverCharacteristicsForService:error:
。
您看到與上一期的類比嗎? 因此,請實現peripheral(_:didDiscoverCharacteristicsFor:error:)
或更明確地實現: optional func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.