简体   繁体   中英

Transferring Data from Central to peripheral in Background mode in iOS

I am developing an application for a custom wearable communicating through BLE.

I have subscribed to the UI background modes in the info.plist file for Bluetooth-central. I am transferring a firmware file of around 600 kb by dividing into chunk sizes of 200 bytes each. The process is going fine but as I am pressing the Home button the app is entering into background state and thus terminating the process after 1-2 minutes.

If my screens dims after certain amount of time then the firmware transfer continues but as soon as the home button is pressed the app stops transferring the data after few minutes.

Please help me out of this scenario.

Thanks.

To run task in background mode, you need to follow the belowed steps.

Step 1: Declare __block UIBackgroundTaskIdentifier bgTask as global variable.

Step 2: To add following code in applicationDidEnterBackground .

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

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

}

Step 3: Stop background task handler once apps come in 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];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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