在 iOS 中以后台模式将数据从 Central 传输到外围设备
Transferring Data from Central to peripheral in Background mode in iOS
我正在为通过 BLE 通信的自定义可穿戴设备开发应用程序。
我已经在 info.plist 文件中为 Bluetooth-central 订阅了 UI 背景模式。
我通过分成每个 200 字节的块大小来传输大约 600 kb 的固件文件。该过程进行得很顺利,但是当我按下主页按钮时,应用程序进入后台状态,因此在 1-2 分钟后终止该过程。
如果我的屏幕在一定时间后变暗,则固件传输会继续,但只要按下主页按钮,应用程序就会在几分钟后停止传输数据。
请帮助我摆脱这种情况。
谢谢。
要运行后台模式任务,您需要按照以下步骤操作。
第 1 步:将 __block UIBackgroundTaskIdentifier bgTask
声明为全局变量。
第 2 步:在 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];
}
我正在为通过 BLE 通信的自定义可穿戴设备开发应用程序。
我已经在 info.plist 文件中为 Bluetooth-central 订阅了 UI 背景模式。 我通过分成每个 200 字节的块大小来传输大约 600 kb 的固件文件。该过程进行得很顺利,但是当我按下主页按钮时,应用程序进入后台状态,因此在 1-2 分钟后终止该过程。
如果我的屏幕在一定时间后变暗,则固件传输会继续,但只要按下主页按钮,应用程序就会在几分钟后停止传输数据。
请帮助我摆脱这种情况。
谢谢。
要运行后台模式任务,您需要按照以下步骤操作。
第 1 步:将 __block UIBackgroundTaskIdentifier bgTask
声明为全局变量。
第 2 步:在 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];
}