iOS 近乎实时地从 Apple Watch 获取心率

标签 ios swift watchkit watchos-2 healthkit

我需要制作一个应用程序来近乎实时地记录心率数据,并尽快将此数据发送到服务器。

首先我采用了这种方法:Watch os 2.0 beta: access heart beat rate

事实上,它运行得很好。 HealthKit 每五秒就会有新的心率数据。但现在我遇到了无法与服务器同步的问题。

我的第一个方法是 Watch 应用。 watch 正在向服务器发送数据。这是行不通的,因为一旦 watch 上的屏幕变黑,它就会停止发送。

我的下一个方法是每五秒查询一次 iPhone 上的 HealthKit 以获取新数据。只要应用程序位于前台,这就有效。

然后我看到有某种后台功能可以监视 HealthKit 本身并从后台撤销应用程序,您可以做一些事情。(enableBackgroundDeliveryForType)这似乎不适用于心率(Apple 文档说就像步骤一样,这不起作用,我想心率就是其中之一)。

我现在被困住了。你知道怎么做吗?我需要一些在 iPhone 上每 5-10 秒执行一次的后台任务。这似乎是不可能的

最佳答案

更新

正如 @BootMaker 所注意到的,Apple 在 WatchOS 3 中为 HKWorkout 应用程序提供了后台模式,因此现在可以使用。您必须运行 HKWorkoutSession,即使应用程序处于后台( watch 上的黑屏),这也会保持实时心率传输

The closest you are going to be is while the watch app is open.

我为什么这么说?

  1. HealthKit 的数据库有两个(一个位于 iPhone,另一个位于 Apple Watch)。它们何时同步是任意的,仅由操作系统决定

  2. 最接近实时的情况是当您没有使用任何密码锁定屏幕时iPhoneApple Watch。 无论哪种方式,都不能保证每次将新测量添加到 Apple Watch 的 HealthKit

  3. 时都会发生同步
  4. 强制心率传感器实时工作的唯一方法是通过锻炼观察者,而您>Apple Watch 应用位于前台

  5. 后台交付适用于 Apple Watch 应用。

  6. Watch OS 2 请求传感器至少每 10 分钟自动测量一次(在后台)。

There's no other workaround, if you need real time for longer periods, or while the user is not using your app, you will need to use an specialized wearable.

关于iOS 近乎实时地从 Apple Watch 获取心率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36675692/

相关文章:

iOS:使用 Swift 创建对象类

ios - 发布新版本是否会从Appstore中删除现有版本?

ios - swift 3 : Tableview datasource methods "Overriding non-open instance method outside of its defining module" error

ios - didReceiveApplicationContext 未被调用

ios - 不上传 watch 套件应用程序

iphone - 当应用程序进入后台时关闭弹出窗口

ios - 无法将解析的数据保存到 URLSession 到 Core Data 上下文(iOS 10、Swift 3)

ios - Cloud Kit 在 Swift 2.0 中的 performQuery

swift - 函数返回函数并调用在 Swift 中使用数组获取 Variadic 参数的函数

WatchKit/Healthkit - 无法创建游泳 HKWorkout 类(class)