iOS BLE后台重连
iOS BLE background reconnection
我在后台重新连接设备时遇到问题。当我离开 BLE 设备区域时,离开 iPhone 大约 3 分钟并等待后台然后返回,它不会重新连接。我尝试在后台扫描外围设备,但即使我指定了 UUID,它也无法正常工作。有什么解决办法吗?
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
dispatch_async(dispatch_get_main_queue(), {
self.centralManager?.connectPeripheral(self.choosenPeripheral!, options: nil)
})
}
当外设断开时,只需要在didDisconnectPeripheral
委托方法中再次调用connectPeripheral
即可;这将创建一个 "pending" 连接,一旦外设返回范围 iOS 将连接到它并调用你的 didConnectPeripheral
委托方法。
您不需要 Dispatch
连接操作。只需使用:
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
central.connectPeripheral(peripheral, options: nil)
}
我在后台重新连接设备时遇到问题。当我离开 BLE 设备区域时,离开 iPhone 大约 3 分钟并等待后台然后返回,它不会重新连接。我尝试在后台扫描外围设备,但即使我指定了 UUID,它也无法正常工作。有什么解决办法吗?
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
dispatch_async(dispatch_get_main_queue(), {
self.centralManager?.connectPeripheral(self.choosenPeripheral!, options: nil)
})
}
当外设断开时,只需要在didDisconnectPeripheral
委托方法中再次调用connectPeripheral
即可;这将创建一个 "pending" 连接,一旦外设返回范围 iOS 将连接到它并调用你的 didConnectPeripheral
委托方法。
您不需要 Dispatch
连接操作。只需使用:
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
central.connectPeripheral(peripheral, options: nil)
}