Transferring data from central to peripheral in the background in iOS

I am developing an application for custom portable communication over BLE.

I have subscribed to background UI in the info.plist file for bluetooth-central. I am transferring a firmware file of about 600 kb, divided into chunks of 200 bytes each. The process is going well, but when I press the Home button, the application goes into the background and thus exits the process after 1-2 minutes.

If my screens dim after a certain period of time, the firmware transfer continues, but as soon as the home button is pressed, the application stops transferring data after a few minutes.

Please help me with this scenario.

Thank.

+1


source to share


1 answer


To run a task in the background, you need to follow these steps.

Step 1: declare __block UIBackgroundTaskIdentifier bgTask

as a global variable.

Step 2: To add the following code to applicationDidEnterBackground

.



- (void)applicationDidEnterBackground:(UIApplication *)application {

    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    bgTask = UIBackgroundTaskInvalid;
}];

}

      

Step 3: Stop the background handler when applications enter foreground mode.

- (void)applicationWillEnterForeground:(UIApplication *)application {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

      [[UIApplication sharedApplication] endBackgroundTask:bgTask];

}

      

+2


source







All Articles