Swift - 后台蓝牙写入特性

Swift - Bluetooth write characteristic in Background

我正在 Swift 5 中开发一个应用程序,它连接到可测量 body 信息(温度、心率等)的可穿戴设备

为了测量此信息,我必须在设备的一个特性中写入数据 object(并在激活通知的情况下接收其他特性中的信息),现在我必须每 30 分钟测量一次,所以我必须创建一个定时器,每 30 分钟重复一次,以在 BLE 特性中执行该“写入”。 (Capabilities 中的所有选项都已激活 - bluetooth-central、bluetooth-peripheral、background-processing、background-fetch)

我在 Stack Overflow 中阅读了很多主题,有些人说你不能在后台写入 BLE,而其他人说你可以。

在实施我读过的所有可能性之前:可以每 30 分钟执行一次连接到设备的任务,将一个特征的通知设置为 true,并在其中写入一个数据 object背景模式下的特征?

如果响应为“是”,除了已经在前台运行的相应蓝牙方法之外,是否有任何我必须实现的代码?

我没有附上我的 BLEManager class (centralManager),因为它是 Apple 文档中的“标准”,但如果您需要,请问我,我会附上它。

可以在后台模式下使用 CoreBluetooth:https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

注意这里的一些注意事项:

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.

我觉得你可以直接用外设唤醒应用,但是调度可能不行。

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

不确定你想做什么,但你的时间有限,所以请记住这一点。