核心蓝牙和后台处理:即使在后台模式下工作几天,也可以检测设备并触发操作? [英] Core Bluetooth and backgrounding: Detection of a device and triggering an action, even after being days in background mode?

查看:174
本文介绍了核心蓝牙和后台处理:即使在后台模式下工作几天,也可以检测设备并触发操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个应用程序,当某个蓝牙低功耗设备进入范围内时需要获得通知。如果BLE设备被注意到,我的应用程序只存储时间戳。

I've written an app that needs to get informed when a certain Bluetooth Low Energy device comes within range. If the BLE device gets noticed my app just stores a timestamp.

如WWDC 2012核心蓝牙视频中所述,应用程序在后台模式下有两种可能性使用Core Bluetooth:

As stated in the WWDC 2012 Core Bluetooth videos, there are two possibilities for apps for acting in background mode when working with Core Bluetooth:


事件背景

事件背景可能是大多数应用程序
在与蓝牙低功耗设备交互时将使用的内容。这个
模式不允许在应用程序处于后台的
时直接与附件通信,但是当它想要与应用程序通信时确实提供来自
附件的通知。当你的应用程序处于
背景时,iOS将
保持与BTLE附件的连接,并将继续监控通知。当
连接的BTLE配件有可用通知时,iOS会通知
用户该配件想要与您的应用程序通信,允许用户加b $ b用户加载您的应用程序并与配件交互。由于许多
设备需要节省电量,仅提供
确定时间的信息将大大提高
配件和iPhone 4S的电池寿命。

Event backgrounding is probably what most Apps will use when interacting with Bluetooth Low Energy devices. This mode does not allow for direct communications to the accessory when the App is in the background, but does provide for a notification from the accessory when it wants to communicate with the app. iOS will stay connected to the BTLE accessory when your App is in the background and will continue to monitor for notifications. When the connected BTLE accessory has a notification available, iOS will notify the user that the accessory would like to talk to your App, allowing the user to load your App and interact with the accessory. As many devices need to conserve power, only providing information at deterministic times will greatly enhance the battery life of the accessory and the iPhone 4S.


  • 此模式不需要info.plist条目。

会话背景

有时候应用程序必须与
交互一个附件,即使它在后台运行。考虑运行需要实时监控心率的
运行应用。这个模型有一个
清除START和STOP。用户在
应用程序中运行它们。当运行处于活动状态时,应用程序会读取心率信息
,直到运行完成或停止。会话后台还
允许扫描和连接到BTLE配件,而App
在后台。即使应用程序位于
后台,scanForPeripheralsWithServices或
connectPeripheral调用也将继续。 CoreBluetooth将继续监控特定的
外围设备或外围设备,这些外围设备或外围设备与您的应用程序正在寻找的服务相匹配,并在找到或连接时呼叫您的应用程序代表。请注意,每当BTLE外围设备或iPhone 4S使用其无线电时,
耗尽相应设备的可用功率。应用程序开发人员
使用基于会话的后台处理必须注意电源使用情况。

There are times where an App must interact with an accessory even if it is running in the background. Consider a running app that needs to monitor heart rate real-time. There is a clear START and STOP to this model. The user STARTS their run in the App. While the run is active, the App reads heart rate information until the run is completed or STOPPED. Session backgrounding also allows for scanning and connection to BTLE accessories while the App is in the background. A scanForPeripheralsWithServices or connectPeripheral call will continue, even when the App is in the background. CoreBluetooth will continue to monitor for specific peripherals or peripherals that match the services your App is looking for and call your Apps delegate when found or connected. Be mindful, that every time a BTLE peripheral or iPhone 4S uses its radio, it is depleting the respective devices available power. App developers using session based backgrounding must be mindful of power usage.


  • 会话后台处理需要后台模式进入UIBackgroundModes,蓝牙-central,在你的应用程序info.plist中。

到目前为止我的会话背景(与info.plist条目)。该应用程序要求iOS检索所有已知设备,然后将connect命令提供给我正在寻找的设备。连接回调即使在我的应用程序被搁置后数分钟之后也会出现。

Until now I Session Backgrounding (with the according info.plist entry). The app asks iOS to retrieve all known devices and then gives the connect command to the device I'm looking for. The connection callback comes even after minutes after my app was backgrounded.

但是:应用程序在 - 让我们说 - 一小时后被暂停。这意味着下次我的应用程序被用户启动时,它无法判断是否有任何感兴趣的BLE设备。

But: the app gets suspended after - let's say - one hour. This means that the next time my app gets launched by the user, it cannot tell if there have been any sightings of the BLE device of interest.

所以我的问题是:当我的应用程序在某个BLE设备进入后台时被发送到后台几天后,即使没有用户交互也可以获得通知,这样我就可以存储我的时间戳了吗?

So my question is: Is it possible for my app to get notified without user interaction even days after being sent to background when a certain BLE device comes in range so I can store my time stamp?

推荐答案

不,iOS不保证您的应用在后台保持活力。文档说:

No, iOS gives no guarantee that your app keeps alive in the background. The docs say:


但是,在应用程序在后台运行(未暂停)和系统的情况下,可能会调用此方法由于某种原因需要终止它。

However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.

applicationWillTerminate

这篇关于核心蓝牙和后台处理:即使在后台模式下工作几天,也可以检测设备并触发操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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