使用 HKHeartbeatSeriesSample 从 Apple Watch 获取实时心跳数据

Real time heart beat data from Apple Watch using HKHeartbeatSeriesSample

我正在尝试实时访问从 Apple Watch 记录的心跳数据。使用 HKWorkoutSession,我可以每 5 秒获得一次 workoutBuilder didCollectDataOf 心率 (beats/min) 数据的回调。

这对我的应用程序来说不够实时,所以我正在研究使用新的(从 iOS 13 和 watchOS 6.0 开始)HKHeartbeatSeriesSample(和相关的 类)这个目的。我的理解是,心跳数据的系列样本将以系列形式记录逐跳测量。

我能够让 APIs 工作并获得一些数据,但不是实时的——我获得的系列数据来自一些以前的记录(不清楚是什么触发了这些记录)。

授权如下:

            let allTypes: Set<HKSampleType> = Set([
                HKObjectType.workoutType(),
                HKSeriesType.heartbeat(),
                HKObjectType.quantityType(forIdentifier: .heartRate)!,
                HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)!,
            ])

            healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
                ///...
                let workoutSession = WorkoutSession(healthStore: self.healthStore)
                workoutSession.startHeartbeatSampleQuery()
            }

startHeartBeatSampleQuery:

            let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
                                                  ascending: false)

            // Query for the heartbeat samples from the specified heartbeat series.
            let heartbeatSeriesSampleQuery = HKSampleQuery(sampleType: HKSeriesType.heartbeat(),
                                                           predicate: nil,
                                                           limit: HKObjectQueryNoLimit,
                                                           sortDescriptors: [sortDescriptor]) {
                (query, results, error) in

                guard let samples = results, let sample = samples.first as? HKHeartbeatSeriesSample else {
                    print("NO SAMPLES MY FRIEND")
                    return
                }

                let heartbeatSeriesQuery = HKHeartbeatSeriesQuery(heartbeatSeries: sample) {
                    (query, timeSinceSeriesStart, precededByGap, done, error) in

                    guard error == nil else {
                        print("error in HKHeartbeatSeriesQuery: \(String(describing: error))")
                        return
                    }

                    print("timeSinceSeriesStart = \(timeSinceSeriesStart), precededByGap = \(precededByGap)")

                }

                self.healthStore.execute(heartbeatSeriesQuery)
            }

            completionHandler()

            self.healthStore.execute(heartbeatSeriesSampleQuery)
        }

我从这个查询中得到的系列样本:

samples: [count=41 5D4F0C84-294D-41A7-8F64-B387A8E767A3 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-18 12:12:06 -0800 - 2020-01-18 12:13:12 -0800), count=27 32B0B090-CDF8-48F5-BCF6-670982C426F3 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-15 21:38:41 -0800 - 2020-01-15 21:39:46 -0800), count=67 B485328A-3EA1-49F2-8666-75B3B2143A05 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-14 21:00:15 -0800 - 2020-01-14 21:01:17 -0800), count=48 9D550B3A-F325-4CA0-B5E7-43BA42E835ED "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-14 17:06:27 -0800 - 2020-01-14 17:07:33 -0800), count=50 278E1BF7-726B-443B-97C3-8BBA3DF06207 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-13 12:04:28 -0800 - 2020-01-13 12:05:34 -0800), count=33 1215B2C4-F168-4EAD-9C35-5F734CC29637 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-13 11:55:01 -0800 - 2020-01-13 11:56:06 -0800), count=48 EB2C64F9-9E81-4F5B-BA4A-62FA6816FE29 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-11 12:20:36 -0800 - 2020-01-11 12:21:37 -0800), count=46 515D467A-51E9-490B-8082-5C8F541A0BD7 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-11 11:01:38 -0800 - 2020-01-11 11:02:44 -0800)], sample: count=41 5D4F0C84-294D-41A7-8F64-B387A8E767A3 "Vishal’s Apple Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-18 12:12:06 -0800 - 2020-01-18 12:13:12 -0800)

最近的是 1/18,与我的活跃锻炼会话不对应 -- 无论我是开始 HKWorkoutSession,还是在我的手表上手动锻炼,这似乎都没有改变.

但是系列中的数据正是我想要的;逐拍数据,如:

timeSinceSeriesStart = 1.1875, precededByGap = false
timeSinceSeriesStart = 2.046875, precededByGap = false
timeSinceSeriesStart = 2.92578125, precededByGap = false
timeSinceSeriesStart = 3.8125, precededByGap = false
...

我使用 API 不正确吗?或者这不是获取实时心跳数据的可行方法?

没有一种方法可以比您在锻炼过程中观察到的更精确地记录心率 API。 HKSeriesType.heartbeat() 类型的样本是在 Apple Watch 计算心率变异性时记录的(参见样本类型 heartRateVariabilitySDNN)。 Apple Watch 在后台定期计算 HRV。没有 API 用于启动 HRV 读数。