简体   繁体   English

iOS BLE后台重新连接

[英]iOS BLE background reconnection

I have a problem with background reconnection with device. 我遇到了与设备重新连接的问题。 When I leave BLE device area, leave the iPhone for about 3 min and wait for background then go back, it won't reconnect. 当我离开BLE设备区域时,离开iPhone大约3分钟并等待背景然后返回,它将不会重新连接。 I tried to scan for peripheral in background but it isn't working even when I specified UUID. 我试图在后台扫描外围设备,但即使我指定了UUID也无法正常工作。 Is there any solution for that? 那有什么解决方案吗?

func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        dispatch_async(dispatch_get_main_queue(), {
            self.centralManager?.connectPeripheral(self.choosenPeripheral!, options: nil)  
        })    
   }

When the peripheral disconnects, you simply need to call connectPeripheral again in the didDisconnectPeripheral delegate method; 当外设断开连接时,您只需要在didDisconnectPeripheral委托方法中再次调用connectPeripheral ; This will create a "pending" connection and as soon as the peripheral comes back into range iOS will connect to it and call your didConnectPeripheral delegate method. 这将创建一个“待定”连接,一旦外围设备返回范围,iOS将连接到它并调用您的didConnectPeripheral委托方法。

You don't need to Dispatch the connect operation. 您不需要Dispatch连接操作。 Just use: 只需使用:

func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        central.connectPeripheral(peripheral, options: nil)    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM