【问题标题】:Transferring Data from Central to peripheral in Background mode in iOS在 iOS 的后台模式下将数据从 Central 传输到外围设备
【发布时间】:2015-02-26 10:51:37
【问题描述】:

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

我已经订阅了蓝牙中心的 info.plist 文件中的 UI 后台模式。 我通过划分为每个 200 字节的块大小来传输大约 600 kb 的固件文件。该过程进展顺利,但当我按下主页按钮时,应用程序进入后台状态,因此在 1-2 分钟后终止该过程。

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

请帮助我摆脱这种情况。

谢谢。

【问题讨论】:

    标签: ios iphone xcode bluetooth-lowenergy background-process


    【解决方案1】:

    要在后台模式下运行任务,您需要按照以下步骤操作。

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

    【讨论】:

    • 谢谢老兄!!!我认为它现在正在工作。我会接受你的回答,但需要进行更多测试。
    猜你喜欢
    • 2012-01-11
    • 2016-04-26
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2013-01-19
    • 2013-10-19
    相关资源
    最近更新 更多