Core Bluetooth - 在后台执行长期操作
Core Bluetooth - Performing Long-Term Actions in the Background
这是来自 iOS 应用程序部分的核心蓝牙后台处理,可在官方文档中找到:
在后台执行长期操作
Some apps may need to use the Core Bluetooth framework to perform
long-term actions in the background. As an example, imagine you are
developing a home security app for an iOS device that communicates
with a door lock (equipped with Bluetooth low energy technology). The
app and the lock interact to automatically lock the door when the user
leaves home and unlock the door when the user returns—all while the
app is in the background. When the user leaves home, the iOS device
may eventually become out of range of the lock, causing the connection
to the lock to be lost. At this point, the app can simply call the
connectPeripheral:options: method of the CBCentralManager class, and
because connection requests do not time out, the iOS device will
reconnect when the user returns home.
好的,我们有一个应用 locks/unlocks 适当的门...正如所指出的,当应用在后台时(很可能处于挂起模式),这会起作用。现在,让我们继续(引用文档):
Now imagine that the user is away from home for a few days. If the app
is terminated by the system while the user is away, the app will not
be able to reconnect to the lock when the user returns home, and the
user may not be able to unlock the door. For apps like these, it is
critical to be able to continue using Core Bluetooth to perform
long-term actions, such as monitoring active and pending connections.
因此,如果用户几天不在家,并且应用已被 iOS 终止,我们将不得不实施状态保存和恢复,以便 iOS 重新启动当应用程序检测到连接请求时,让应用程序解锁门。相关语录:
In the case of the home security app described above, the system
would monitor the connection request, and re-relaunch the app to
handle the centralManager:didConnectPeripheral: delegate callback when
the user returned home and the connection request completed.
这都是有道理的,但是再注意一下这部分:
Now imagine that the user is away from home for a few days. If the app
is terminated by the system while the user is away, the app will not be able to reconnect to the lock when the user returns home, and
the user may not be able to unlock the door. For apps like these, it
is critical to be able to continue using Core Bluetooth to perform
long-term actions...
这是否意味着,如果应用程序在用户不在家的某个时刻被强行杀死,这是否也会起作用?意味着当用户回家时,门无论如何都会解锁,或者他必须手动重新启动应用程序才能解锁门?
我问这个,因为终止应用程序的重新启动是如何工作的。用户杀掉应用和iOS杀掉支持后台执行的应用不一样:
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...
所以再一次,如果用户离开了几天并且他通过双击主页按钮并向上拖动关闭了应用程序,他是否能够在不手动重新启动应用程序的情况下进入他的家?
没有。如果应用程序被用户强行杀死,则它不会再次被唤醒。唯一会被唤醒的情况是应用程序被 iOS 本身终止,当应用程序有一段时间没有处于前台时,这种情况迟早会发生。如果设备重新启动,它也不会重新启动。
话虽如此,根据我使用 Core Bluetooth 的经验,我得出的结论是状态保存太不可靠了。我相信您尝试实现的用例将无法正常工作,这具有讽刺意味,因为这正是 Apple 在其文档中推广的用例。
例如,您将遇到以下问题:
如果事件源自您正在与之通信的外围配件,例如 connect/disconnect 事件和特征通知,状态恢复将仅因蓝牙相关 activity 重新启动您的应用程序。对于其他事件,最重要的一般蓝牙状态更改事件,您的应用程序不会重新启动并收到通知。之所以如此糟糕,是因为任何蓝牙状态更改事件都会导致所有挂起的连接被丢弃,这意味着您与门锁的挂起连接将丢失。但是,由于您的应用程序不会重新启动以收到此通知,因此这实际上意味着您的应用程序仍会认为连接仍在挂起,而实际上并非如此。由于此时您的应用程序已终止,因此再次唤醒它的唯一方法是让用户再次手动启动它(或者为此目的“破解”其他后台模式,这也不会非常可靠地工作)。
如果用户切换飞行模式、切换蓝牙、重新启动 iOS 设备,或任何其他许多导致状态变化的未定义原因,就会发生这种情况……而且蓝牙状态不太可能-如果“...用户离开家几天”,则不会发生更改。
这个 "issue" 已被我和其他人多次报告,但 Apple 出于某种原因似乎不想修复它。
除此之外,还存在许多其他问题,例如 XPC 连接在不同时间无故中断。我还注意到挂起的连接可以进入“边缘状态”模式,其中外围设备状态设置为连接,但实际上它永远不会连接,除非您循环连接状态。等等等等……
/A
这是来自 iOS 应用程序部分的核心蓝牙后台处理,可在官方文档中找到:
在后台执行长期操作
Some apps may need to use the Core Bluetooth framework to perform long-term actions in the background. As an example, imagine you are developing a home security app for an iOS device that communicates with a door lock (equipped with Bluetooth low energy technology). The app and the lock interact to automatically lock the door when the user leaves home and unlock the door when the user returns—all while the app is in the background. When the user leaves home, the iOS device may eventually become out of range of the lock, causing the connection to the lock to be lost. At this point, the app can simply call the connectPeripheral:options: method of the CBCentralManager class, and because connection requests do not time out, the iOS device will reconnect when the user returns home.
好的,我们有一个应用 locks/unlocks 适当的门...正如所指出的,当应用在后台时(很可能处于挂起模式),这会起作用。现在,让我们继续(引用文档):
Now imagine that the user is away from home for a few days. If the app is terminated by the system while the user is away, the app will not be able to reconnect to the lock when the user returns home, and the user may not be able to unlock the door. For apps like these, it is critical to be able to continue using Core Bluetooth to perform long-term actions, such as monitoring active and pending connections.
因此,如果用户几天不在家,并且应用已被 iOS 终止,我们将不得不实施状态保存和恢复,以便 iOS 重新启动当应用程序检测到连接请求时,让应用程序解锁门。相关语录:
In the case of the home security app described above, the system would monitor the connection request, and re-relaunch the app to handle the centralManager:didConnectPeripheral: delegate callback when the user returned home and the connection request completed.
这都是有道理的,但是再注意一下这部分:
Now imagine that the user is away from home for a few days. If the app is terminated by the system while the user is away, the app will not be able to reconnect to the lock when the user returns home, and the user may not be able to unlock the door. For apps like these, it is critical to be able to continue using Core Bluetooth to perform long-term actions...
这是否意味着,如果应用程序在用户不在家的某个时刻被强行杀死,这是否也会起作用?意味着当用户回家时,门无论如何都会解锁,或者他必须手动重新启动应用程序才能解锁门?
我问这个,因为终止应用程序的重新启动是如何工作的。用户杀掉应用和iOS杀掉支持后台执行的应用不一样:
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...
所以再一次,如果用户离开了几天并且他通过双击主页按钮并向上拖动关闭了应用程序,他是否能够在不手动重新启动应用程序的情况下进入他的家?
没有。如果应用程序被用户强行杀死,则它不会再次被唤醒。唯一会被唤醒的情况是应用程序被 iOS 本身终止,当应用程序有一段时间没有处于前台时,这种情况迟早会发生。如果设备重新启动,它也不会重新启动。
话虽如此,根据我使用 Core Bluetooth 的经验,我得出的结论是状态保存太不可靠了。我相信您尝试实现的用例将无法正常工作,这具有讽刺意味,因为这正是 Apple 在其文档中推广的用例。
例如,您将遇到以下问题:
如果事件源自您正在与之通信的外围配件,例如 connect/disconnect 事件和特征通知,状态恢复将仅因蓝牙相关 activity 重新启动您的应用程序。对于其他事件,最重要的一般蓝牙状态更改事件,您的应用程序不会重新启动并收到通知。之所以如此糟糕,是因为任何蓝牙状态更改事件都会导致所有挂起的连接被丢弃,这意味着您与门锁的挂起连接将丢失。但是,由于您的应用程序不会重新启动以收到此通知,因此这实际上意味着您的应用程序仍会认为连接仍在挂起,而实际上并非如此。由于此时您的应用程序已终止,因此再次唤醒它的唯一方法是让用户再次手动启动它(或者为此目的“破解”其他后台模式,这也不会非常可靠地工作)。
如果用户切换飞行模式、切换蓝牙、重新启动 iOS 设备,或任何其他许多导致状态变化的未定义原因,就会发生这种情况……而且蓝牙状态不太可能-如果“...用户离开家几天”,则不会发生更改。
这个 "issue" 已被我和其他人多次报告,但 Apple 出于某种原因似乎不想修复它。
除此之外,还存在许多其他问题,例如 XPC 连接在不同时间无故中断。我还注意到挂起的连接可以进入“边缘状态”模式,其中外围设备状态设置为连接,但实际上它永远不会连接,除非您循环连接状态。等等等等……
/A