我正在为通过 BLE 进行通信的自定义可穿戴设备开发应用程序。
我已经在 info.plist 文件中订阅了 Bluetooth-central 的 UI 背景模式。 我通过分成每个 200 字节的 block 大小来传输大约 600 kb 的固件文件。该过程进展顺利,但当我按下主页按钮时,该应用程序进入后台状态,因此在 1-2 分钟后终止了该过程。
如果我的屏幕在一定时间后变暗,则固件传输会继续,但只要按下主页按钮,应用程序就会在几分钟后停止传输数据。
请帮助我摆脱这种情况。
谢谢。
最佳答案
要在后台模式下运行任务,您需要按照以下步骤操作。
第 1 步:将 __block UIBackgroundTaskIdentifier bgTask
声明为全局变量。
第二步:在applicationDidEnterBackground
中添加如下代码。
- (void)applicationDidEnterBackground:(UIApplication *)application {
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
}
第 3 步:应用进入前台模式后停止后台任务处理程序。
- (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];
}
关于ios - 在 iOS 中以后台模式将数据从 Central 传输到外围设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28740459/