1

The app is in background and it receives a callback upon disconnection with the BLE device, after which the app has to wait for sometime(1minute) and then execute some piece of code. The app behaves as expected even when in the background and is just connected to a system (Mac, Windows or Ubuntu), but not otherwise. This behavior is seen only on iOS 11.3 and not on 10.3.

When the app is in background and it receives a callback when the BLE device is disconnected, app registers for a background task with a timer of 1minute, after which certain api call is to be made.

Below were my observations:

  1. If device is connected to a system, all the background task and api calls are made gracefully.
  2. If device is not connected to a system then the background task and api calls are not executed in iOS 11.3 but works fine on 10.3 (If timer is more than 10seconds works fine on 10.3, but not on 11.3)
  3. When I do a wireless debugging, the connection to the iPhone is automatically lost after a few seconds (>10). (This behavior is observed in 11.3)
  4. When the timer is less than or equal to 10seconds the app works perfect on all the versions and even it is not connected to any system.

This is the code in AppDelegate to start a timer in background:

func startTimerWith(timeInterval: TimeInterval) {
    registerBackgroundTask()
    timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false, block: { (timer) in
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Time"), object: nil)
        self.endBackgroundTask()
    })
}

func registerBackgroundTask() {
    backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundTask()
    })
}

func endBackgroundTask() {
    print("Background task ended.")
    UIApplication.shared.endBackgroundTask(backgroundTask)
    backgroundTask = UIBackgroundTaskInvalid
    timer?.invalidate()
    timer = nil
}

When disconnected from the BLE device, I start the timer by registering to background task:

func disconnected(_ peripheral: CBPeripheral, with error: Error?) {
    print("DISCONNECTED!!!")
    AppDelegate.sharedApp().startTimerWith(timeInterval: TimeInterval(TIME))
    BLEDeviceHandler.sharedInstance.handleBLEDevice(connectedPeripheral!)
}   
Ganesh patro
  • 93
  • 1
  • 5
Megha
  • 41
  • 5

0 Answers0