ios - 在 iOS 中以后台模式将数据从 Central 传输到外围设备

标签 ios iphone xcode bluetooth-lowenergy background-process

我正在为通过 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/

相关文章:

c++ - 在 cocos2d-x 中暂停特定场景

ios - 使用 NSParagraphStyle 从 NSAttributedString 计算高度

ios - 尝试从自定义单元格中恢复 View

iphone - NSDateFormatter 字符串

ios - Angular 2 应用程序 : route content is blank on iOS

xcode - 在 Xcode 9 上切换分支

swift - 使用 PickerView 将卡片图片设置为其名称

ios - 向sqlite数据库中插入数据

ios - _swift_abortRetainUnowned 将@objc 类捕获为无主时

iphone - 密码文本字段的顺序验证