应用程序终止时,BLE设备保持与iPhone的连接 [英] BLE device keep connect with iPhone when application terminate

查看:96
本文介绍了应用程序终止时,BLE设备保持与iPhone的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS上遇到BLE问题. 我的情况如下: 我正在开发连接到设备BLE 4.0的应用程序.我的问题是来电功能.该功能的场景是在设备BLE和应用程序之间连接后,当接到电话时,我的应用程序会将数据发送到BLE设备,使其振动并闪烁.

I got a problem with BLE on iOS. My case as below: I'm developing application connect to device BLE 4.0. My problem is incoming call function. The scenario of this function is after connected between device BLE and application, when phone is received a phone call, my application will send data to BLE device and make it vibrate and light blinking.

当前,我在下面尝试了4种情况,但在3种情况下成功了,其余的情况都失败了:

Current I tried 4 case below, but successful in 3 case and the rest case is fail:

  • 案例1 :(成功)当应用程序在前景上->来电->应用程序可以向BLE设备发送振动和闪烁​​的消息

  • Case 1:(successful) When app on Foreground --> Incoming call --> App can send vibrate and light blinking message to BLE device

案例2 :(成功)当单击主页"并且应用程序转到后台时->来电->应用程序可以向BLE设备发送振动和闪烁​​的消息

Case 2:(successful) When click home and app go to background--> Incoming call --> App can send vibrate and light blinking message to BLE device

案例3 :(成功)当单击家庭和应用程序进入后台并单击睡眠模式按钮->来电->应用程序可以向BLE设备发送振动和闪烁​​的消息

Case 3:(successful) When click home and app go to background and click sleep mode button --> Incoming call --> App can send vibrate and light blinking message to BLE device

情况4 :(失败)当在主屏幕按钮上单击两次并在背景上终止应用程序时(在任务管理器中删除应用程序)->来电->应用程序无法向BLE设备发送振动和闪烁​​的消息

Case 4:(FAIL) When click double on home button and termianate app on background (remove app on task manager)--> Incoming call --> App cannot send vibrate and light blinking message to BLE device

在此之前,有没有人遇到过同样的情况,并且对此有任何经验和想法?

Do anyone get same this case before and have any experience, idea for this help?

感谢您的支持!

推荐答案

在iOS上,有两种方法可以完全终止应用程序:-

On iOS, there are two ways an app is completely terminated:-

  1. 系统终止该应用程序(例如,由于该应用程序已长时间不使用,并且系统将其杀死以节省内存).
  2. 用户终止/杀死该应用.

对于第一种情况,您需要添加对状态保存和恢复的支持.从Apple的文档档案中:-

For the first scenario, you need to add support for State Preservation and Restoration. From Documentation Archive from Apple:-

因为状态保存和恢复内置在Core中 蓝牙,您的应用可以选择启用此功能以要求系统 保留应用的中央和外围管理器的状态,以及 继续在其上执行某些与蓝牙相关的任务 代表,即使您的应用程序不再运行也是如此.当其中之一 任务完成后,系统将您的应用重新启动到后台 并让您的应用有机会恢复其状态并进行处理 该事件.

Because state preservation and restoration is built in to Core Bluetooth, your app can opt in to this feature to ask the system to preserve the state of your app’s central and peripheral managers and to continue performing certain Bluetooth-related tasks on their behalf, even when your app is no longer running. When one of these tasks completes, the system relaunches your app into the background and gives your app the opportunity to restore its state and to handle the event appropriately.

基本上,您需要添加代码以支持此功能,因为默认情况下未启用该功能.请查看下面的链接,特别是增加对状态保存和恢复的支持"部分:-

Basically, you need to add code to support this feature as it is not enabled by default. Please have a look at the link below, specifically the section "Adding Support for State Preservation and Restoration":-

iOS应用的核心蓝牙背景处理

在第二种情况下,如果用户终止/杀死该应用程序,则该应用程序将无法恢复或重新启动.这是出于安全和行为上的原因,如果存在错误或问题并且您强行杀死​​了该应用程序,则您不希望您的应用程序以某种方式重新启动自身,因为否则您将陷入无限循环,并且您将永远无法完全终止有问题的应用程序.请查看下面的链接,该链接进一步说明了这一点:-

For the second scenario, if the user terminates/kills the app, then there is no way for the app to restore or relaunch itself. There is a security and behavioural reason for this in that if there is a bug or an issue and you force killed the app, you wouldn't want your app to somehow relaunch itself, because otherwise you'll end in an infinite loop and you will never be able to fully terminate the buggy app. Please have a look at the following link that further explains this:-

https://www.raywenderlich.com/1395-state-恢复入门指南 https://medium.com/arkulpa/ios-stay-connected-to-external ble-device-as-much-as-possible-699d434846d2

在文档存档中也间接提到了这一点:-

This is also mentioned indirectly in the Documentation Archive:-

支持后台执行的应用可能会被系统重新启动 处理传入事件. 如果某个应用因任何原因终止 除了用户强制退出之外,系统还会启动该应用 当以下事件之一发生时...

Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens...

我希望这会有所帮助.

这篇关于应用程序终止时,BLE设备保持与iPhone的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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