CoreBluetooth 后台执行 10 秒?

CoreBluetooth background execution 10sec?

因为 apple 文档关于 this point 的内容相当模糊:

 Upon being woken up, an app has around 10 seconds to complete a task

这是否意味着应用程序将在 10 秒后返回其 "suspended" 状态?从理论上讲,是否可以 "force" 应用程序在这 10 秒后保持清醒状态? (私人应用)。

在我的例子中,设备将连接到汽车。

试试这个:

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

在 XCode 中,转到您的项目->功能->背景模式并将背景模式标记为打开。 Mark 还使用蓝牙 LE 配件:

Apps that work with Bluetooth peripherals can ask to be woken up if the peripheral delivers an update when the app is suspended. This support is important for Bluetooth-LE accessories that deliver data at regular intervals, such as a Bluetooth heart rate belt. You enable support for using bluetooth accessories from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the bluetooth-central value in your app’s Info.plist file.) When you enable this mode, the Core Bluetooth framework keeps open any active sessions for the corresponding peripheral. In addition, new data arriving from the peripheral causes the system to wake up the app so that it can process the data. The system also wakes up the app to process accessory connection and disconnection notifications.

详情请参考Apple documentation

Does this mean that after 10 sec the app will go back to its "suspended" state ?

如果你仔细阅读:

Upon being woken up, the app has around 10 seconds to process the data. Ideally, it should process the data as fast as possible and allow itself to be suspended again. However, if more time is needed, the app can use the beginBackgroundTaskWithExpirationHandler: method to request additional time; it should do so only when absolutely necessary, though.

应用程序通常通过后台模式(如定位服务、音频、推送通知等)唤醒,对于某些模式(如定位服务),它会保持唤醒状态,直到捕获位置数据,而对于某些模式,它不会像推送通知一样长时间保持清醒。所以这取决于正在执行的任务。对于蓝牙,如果它与另一个蓝牙设备连接,那么它将保持唤醒状态。

Theoretically, is it possible to "force" the app to stay awake after these 10 sec ? (private app).

是的,有可能。

Each of the preceding modes lets the system know that your app should be woken up or launched at appropriate times to respond to relevant events. For example, an app that begins playing music and then moves to the background still needs execution time to fill the audio output buffers. Enabling the Audio mode tells the system frameworks that they should continue to make the necessary callbacks to the app at appropriate intervals. If the app does not select this mode, any audio being played or recorded by the app stops when the app moves to the background.

注意:如果您的私人应用程序需要它,那么您可以查看此 here。如果您添加解决方案并申请应用商店,您的应用将被拒绝。