在iOS中以后台模式将数据从中央传输到外围设备 [英] Transferring Data from Central to peripheral in Background mode in iOS

查看:116
本文介绍了在iOS中以后台模式将数据从中央传输到外围设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经在info.plist文件中为Bluetooth-central预订了UI后台模式. 我正在将大约600 kb的固件文件分成200个字节的块大小进行传输.程序运行正常,但是当我按主页"按钮时,应用程序进入后台状态,因此在1-2分钟后终止了该程序.

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.

谢谢.

推荐答案

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

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

第1步:声明__block UIBackgroundTaskIdentifier bgTask作为全局变量.

Step 1: Declare __block UIBackgroundTaskIdentifier bgTask as global variable.

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

Step 2: To add following code in applicationDidEnterBackground.

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

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

}

步骤3:一旦应用进入前台模式,就停止后台任务处理程序.

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

}

这篇关于在iOS中以后台模式将数据从中央传输到外围设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆