如何使用蓝牙信号 (BLE) 唤醒 iOS 应用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19932090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:52:41  来源:igfitidea点击:

How to wake up iOS app with bluetooth signal (BLE)

iosbluetooth-lowenergycore-bluetoothibeaconcbcentralmanager

提问by DAN

When using the BLE with CoreBluetooth (no iBeacon), is there a way to wake an app that is not running when the device receives a Bluetooth signal?

将 BLE 与 CoreBluetooth(无 iBeacon)一起使用时,有没有办法在设备接收到蓝牙信号时唤醒未运行的应用程序?

I'm simulating a beacon with the RedBearlab's BLE Shield (http://redbearlab.com/bleshield/).

我正在使用 RedBearlab 的 BLE Shield ( http://redbearlab.com/bleshield/)模拟信标。

Thanks, DAN

谢谢,丹

* UPDATE 03/05/14 *

* 更新 03/05/14 *

It looks like Apple has introduced a major update with iOS 7.1: now iOS will open your app for you if it detects a UUID that matches your app. The app only needs to be installed, it doesn't have to be running (logic in AppDelegate needed to answer the wake-up call).

看起来 Apple 已经在 iOS 7.1 中引入了重大更新:现在,如果 iOS 检测到与您的应用匹配的 UUID,它将为您打开您的应用。该应用程序只需要安装,它不必运行(AppDelegate 中的逻辑需要应答唤醒呼叫)。

采纳答案by Khaled Barazi

If the app was running in the background and then closed (I mean here terminated - and you do not see it anymore in the list of background apps when you double click the home button) due to memory pressure from other apps, then the iOS will take care of waking it up again when a delegate is called (assuming you have background modes listed in .plist).

如果应用程序在后台运行然后关闭(我的意思是这里终止 - 当您双击主页按钮时,您在后台应用程序列表中不再看到它)由于来自其他应用程序的内存压力,那么 iOS 将在调用委托时注意再次唤醒它(假设您在 .plist 中列出了后台模式)。

If the app was running in the background and then closed by the user (again I mean here terminated. So the user double clicked to get the list of apps running in the background and then clicked on your app in the background list until it wiggled and then pressed the 'x' next to the app to kill it) then this is a clear indication that the user does not want the app running in the background and the app will not be re-launched. The user has to relaunch the app for its delegates to start functioning again in the background (new in iOS7).

如果应用程序在后台运行然后被用户关闭(我的意思是这里终止。所以用户双击以获取在后台运行的应用程序列表,然后在后台列表中单击您的应用程序,直到它摆动并然后按应用程序旁边的“x”将其杀死),这清楚地表明用户不希望应用程序在后台运行,并且不会重新启动应用程序。用户必须为其代表重新启动应用程序才能在后台再次开始运行(iOS7 中的新功能)。

Update: I am not sure if this is in the documentation but it is in CoreBluetooth WWDC 2013 Video. I recommend you watch it. They spent a good portion of the video on how CoreBluetooth behaves in the background.

更新:我不确定这是否在文档中,但在 CoreBluetooth WWDC 2013 Video 中。我建议你看一下。他们在视频中花了很大一部分时间介绍 CoreBluetooth 在后台的行为。

回答by David Chu

From what I understand, if your app has not previously connected with the BLE Peripheral, then no.

据我了解,如果您的应用之前未与 BLE 外设连接,则不会。

If your app has previously connected with the BLE Peripheral, then use:

如果您的应用之前已连接到 BLE 外设,则使用:

-connectPeripheral:options

-connectPeripheral:选项

Connection requests don't time out. The best place to call this method is when your app loses connectivity with the BLE peripheral. You will get notified when you lose connection to the peripheral in the CBCentralManagerDelegate Protocol:

连接请求不会超时。调用此方法的最佳位置是当您的应用与 BLE 外设失去连接时。当您在 CBCentralManagerDelegate 协议中失去与外围设备的连接时,您将收到通知:

-centralManager: didDisconnectPeripheral: error

-centralManager:didDisconnectPeripheral:错误

So the next time your App comes in range of the BLE Peripheral, it will trigger this method. Also note that you will need to set up State Preservation and Restoration when you initialize a CBCentralManager.

所以下次你的应用程序进入 BLE 外设的范围时,它会触发这个方法。另请注意,当您初始化 CBCentralManager 时,您将需要设置状态保存和恢复。

回答by Andrew

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

the system wakes up your app when any of the CBCentralManagerDelegate or CBPeripheralDelegate delegate methods are invoked, allowing your app to handle important central role events, such as when a connection is established or torn down, when a peripheral sends updated characteristic values, and when a central manager's state changes.

To perform certain peripheral role tasks while in the background, you must include the UIBackgroundModes key with the bluetooth-peripheral value in your app's Info.plist file. When this key-value pair is included in the app's Info.plist file, the system wakes up your app to process read, write, and subscription events.

当调用任何 CBCentralManagerDelegate 或 CBPeripheralDelegate 委托方法时,系统会唤醒您的应用程序,从而允许您的应用程序处理重要的中心角色事件,例如连接建立或断开时,外围设备发送更新的特征值时,以及中央管理器的状态变化。

要在后台执行某些外围角色任务,您必须在应用的 Info.plist 文件中包含 UIBackgroundModes 键和 bluetooth-peripheral 值。当此键值对包含在应用程序的 Info.plist 文件中时,系统会唤醒您的应用程序以处理读取、写入和订阅事件。

回答by mmad

You could try to declare voip in info.plist. For the time being, my application was automatically relaunched after a time, even user terminated it.

您可以尝试在 info.plist 中声明 voip。目前,我的应用程序在一段时间后自动重新启动,甚至用户终止了它。