我正在开发一个连接到BLE外设的应用程序,我有时遇到一个非常奇怪的问题,CentralManager从未找到外设。但是,当在设备上拉下通知中心或向上滑动控制中心时,外围设备会立即显示并连接。
我试图找到这个问题的原因,但到目前为止我还没找到任何东西。除了willResignActive
和didBecomeActive
之外,似乎没有其他生命周期函数被调用(AFAIK),但在这些函数中,除了打印它们之外我还做了其他任何事情。
我已经确定使用self.centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
以防万一我搞砸了并且没有检测到初始扫描。
有谁知道这可能是什么原因,以及控制中心或通知中心对应用程序的影响如何解决这个问题?
谢谢!
编辑:一些可能有助于发现问题的额外代码。但是,我知道问题发生时,discoveredPeripheral
和connectedPeripheral
都是nil
。
didConnect
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
if centralManager.isScanning {
centralManager.stopScan()
}
self.connectedPeripheral = peripheral
self.discoveredPeripheral?.delegate = self
debugPrint("\(Date()): Connected \(peripheral)")
peripheral.discoverServices(nil)
DispatchQueue.main.async {
self.rssiTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.readPeripheralRSSI), userInfo: nil, repeats: true)
}
}
didDiscover
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
if peripheral.name == global.qnr && discoveredPeripheral == nil {
centralManager.stopScan()
debugPrint(
"Discovered \(peripheral). " +
"Initiating authentication sequence with \n" +
"\tQNR: \(global.qnr)\n" +
"\tSessionKey: \(global.sessionKey)\n" +
"\tToken: \(global.bluetoothToken)")
if self.discoveredPeripheral == nil || self.discoveredPeripheral != peripheral {
self.discoveredPeripheral = peripheral
self.centralManager.connect(peripheral, options: nil)
}
}
}
startScanning
func scanForPeripherals() {
if !centralManager.isScanning && global.sessionKey != "" {
let services = [CONSTANTS.CBUUID]
self.centralManager.scanForPeripherals(withServices: services, options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
}
}
答案 0 :(得分:0)
您必须在发现设备时通知代理人。这就是我的处理方式。我发现然后添加或重新添加时删除以前添加的设备。将发现的设备添加到阵列后,如果使用tableView显示,则重新加载表。
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if (peripheral.name != nil) {
// remove previously added devices
items.removeAll()
items.append(peripheral)
}
// Set RSSI to custom peripheral signal property
customPeripheral.signal = RSSI
// Notify delegate for change in data.
NotificationCenter.default.post(name: .reload, object: nil)
}
// Extension for reload tableView when peripheral array is updated
extension Notification.Name {
static let reload = Notification.Name("reload")
}
在你的情况下。扫描完成并找到设备后,您会通知通知连接到您的设备。
// Connect with peripheral
func connectPeripheral(peripheral: CBPeripheral) {
centralManager.connect(peripheral, options: nil)
centralManager.stopScan()
print("Connecting")
}