7

在开发使用蓝牙低功耗的应用程序时,iOS 设备有时会失去与外围设备的连接。(有时几个小时。)

为了重新连接到现有外围设备,应用程序必须全天以特定速率在后台持续扫描,即使应用程序处于后台也是如此。

问题是,由于内存限制等原因,iOS 不能保证您的应用程序不会被杀死。

iPhone OS 编程指南中的信息指出:

如果外围设备在应用程序暂停时提供更新,则使用蓝牙外围设备的应用程序可以要求唤醒。这种支持对于定期传输数据的蓝牙配件很重要,例如蓝牙心率带。当应用程序在其 Info.plist 文件中包含带有 bluetooth-central 值的 UIBackgroundModes 键时,Core Bluetooth 框架会保持打开相应外围设备的所有活动会话。此外,来自外围设备的新数据会导致系统唤醒应用程序,以便它可以处理数据。系统还会唤醒应用程序以处理附件连接和断开通知。

当手机连接到设备并且应用程序处于后台时,不会出现此问题。但是,当设备断开连接并且应用程序处于后台时,它确实会发生。在这种特定情况下,手机不再连接到外围设备,因此不再收到通知。

很多人之前在 Stack Overflow 或 Apple 论坛上都讨论过这个问题,我相信其中一位 Apple 开发人员已经回复说:

我们已经意识到这个问题,并正在尝试提出解决方案。目前,没有解决方法。”

我的问题是,有没有办法至少提高你不会因为内存限制而被 iOS 杀死的机会?

例如,即时通讯应用程序 (IMO) 似乎在后台运行得非常好。在几天和几天不使用后,应用程序将唤醒并显示一条 gChat 消息。

我在质疑诸如

  • 强指针
  • 总内存大小
  • 减少应用程序后台或最小化时的内存大小
  • 降低后台操作频率
  • 等等。
4

3 回答 3

1

Why do you need background execution even when the bluetooth hardware is disconnected? I don't think that you need to "rescan continuously" to reconnect again, if the hardware is "paired" with the iPhone/iPad, it will reconnect itself. Like a bluetooth headset. Or not?

AFAIK you have no chances to accomplish what you are asking for. A normal App is always suspended when the user go back to home. The app has approx. 5 secs of background time to stop timers, save state ecc ecc. There are special background modes that allows you to have more background time, and each of this mode (explained in the page you linked) has a different behavior.

About the bluetooth mode: The descripted behavior is not an issue, but it's by design:

  • the app is suspended

  • when the app is suspended, it can be killed by the OS to free ram (and there are no tricks to avoid this), but the system will wake up if needed.

  • the app is then awaken every time a notification is received (awaken from suspended state or lauched from "previously-killed" state)

  • the app has 10 seconds to do tasks (save informations, ecc ecc). Moreover, can request +10 mins. of background time for a particular task

  • after the 10 secs (or 10 min) the app is suspended again

The example you wrote about the chat app is incorrect: chat apps usually doesn't use any background mode, simply they forward you messages using push notifications. When you open the app, the app connect to a server that stores all your messages and download it.

You can get "more uptime" using location background mode (routing app can work in background), or using a combination of significative location changes (the app is awaken) and the 10 minutes background time, but I think that Apple will reject an app that "abuse" this.

Shortly, you have to design your app to support this behavior.

于 2013-01-07T17:07:47.410 回答
0

从苹果文档中,我一直假设以下内容:

connectPeripheral:options:
Establish a connection to the peripheral.

- (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options;
Parameters
peripheral
The peripheral to connect to.

options
A dictionary to customize the behavior of the connection. See CBConnectPeripheralOptionNotifyOnDisconnectionKey.

Discussion
**This never times out**. Use cancelPeripheralConnection: to cancel a pending connection.'

重要的部分在于这永远不会超时。我认为这将移交给系统,以便它在进入范围时自动连接到外围设备,从而消除了对完整背景的需要。如果我错了,有人纠正我!

于 2013-01-08T07:51:42.410 回答
0

我在更多 Apple 文档中发现了这一点:

后台应用程序的内存使用情况

每个应用程序都应该在进入后台时释放尽可能多的内存。系统会尝试在内存中同时保留尽可能多的应用程序,但是当内存不足时,它会终止暂停的应用程序以回收该内存。在后台消耗大量内存的应用程序是最先被终止的应用程序。

实际上,您的应用程序应该在不再需要对象时立即删除对对象的强引用。删除强引用使编译器能够立即释放对象,以便可以回收相应的内存。但是,如果您想缓存一些对象以提高性能,您可以等到应用程序转换到后台后再删除对它们的引用。

您应该尽快删除强引用的一些对象示例包括:

图像对象

您可以从磁盘再次加载的大型媒体或数据文件 您的应用不需要并且以后可以轻松重新创建的任何其他对象 为了帮助减少您的应用的内存占用,系统会在您的应用移动时自动清除代表您的应用分配的一些数据到背景。

系统清除所有核心动画层的后备存储。这项工作不会从内存中删除应用程序的图层对象,也不会更改当前图层属性。它只是防止这些图层的内容出现在屏幕上,鉴于应用程序在后台,无论如何都不应该发生这种情况。它会删除对缓存图像的任何系统引用。(如果您的应用程序没有对图像的强引用,它们随后会从内存中删除。)它会删除对其他一些系统管理的数据缓存的强引用。

于 2013-01-07T18:31:10.577 回答