在iOS中将背景模式中的数据从中央传输到外围设备

我正在开发一个通过BLE进行自定义可穿戴通信的应用程序。

我订阅了蓝牙中央的info.plist文件中的UI背景模式。 我正在传输一个大约600 kb的固件文件,分成200个字节的块大小。 这个过程是正常的,但是当我按Homebutton时,应用程序正在进入后台状态,从而在1-2分钟后终止进程。

如果我的屏幕在一段时间后变暗,则固件传输将继续,但只要按下主页button,应用程序将在几分钟后停止传输数据。

请帮我摆脱这种情况。

谢谢。

要在后台模式下运行任务,您需要执行以下步骤。

第1步:声明__block UIBackgroundTaskIdentifier bgTask作为全局variables。

第2步:在applicationDidEnterBackground添加以下代码。

 - (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ bgTask = UIBackgroundTaskInvalid; }]; } 

第三步:一旦应用程序进入前台模式,停止后台任务处理程序。

 - (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]; }