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. I tried to scan for peripheral in background but it isn't working even when I specified 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)
})
}
didDisconnectdelegate method you should immediately callconnecton the peripheral. Then, when it comes back into range you will reconnect to it. - Paulw11dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)- DaloteonDispatch. All you need iscentralManager.connectPeripheral(peripheral, options: nil). Make sure you have the appropriate Bluetooth background mode enabled on your app capabilities. You also need to opt in to Bluetooth state restoration for long-term background operations so that iOS can relaunch your app if required. Do not use aUIViewControllerto hold yourCBCentralManagerand related properties. Use another class that is held by your AppDelegate or implemented as a Singleton. - Paulw11