使用 HKHeartbeatSeriesSample 来自 Apple Watch 的实时心跳数据

fig*_*ump 4 ios swift healthkit apple-watch watchkit

我正在尝试实时访问 Apple Watch 记录的心跳数据。通过 HKWorkoutSession,我可以每 5 秒回调一次workoutBuilder didCollectDataOf心率(心跳/分钟)数据。

\n\n

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

\n\n

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

\n\n

这里是授权:

\n\n
            let allTypes: Set<HKSampleType> = Set([\n                HKObjectType.workoutType(),\n                HKSeriesType.heartbeat(),\n                HKObjectType.quantityType(forIdentifier: .heartRate)!,\n                HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)!,\n            ])\n\n            healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in\n                ///...\n                let workoutSession = WorkoutSession(healthStore: self.healthStore)\n                workoutSession.startHeartbeatSampleQuery()\n            }\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

startHeartBeatSampleQuery:

\n\n
            let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,\n                                                  ascending: false)\n\n            // Query for the heartbeat samples from the specified heartbeat series.\n            let heartbeatSeriesSampleQuery = HKSampleQuery(sampleType: HKSeriesType.heartbeat(),\n                                                           predicate: nil,\n                                                           limit: HKObjectQueryNoLimit,\n                                                           sortDescriptors: [sortDescriptor]) {\n                (query, results, error) in\n\n                guard let samples = results, let sample = samples.first as? HKHeartbeatSeriesSample else {\n                    print("NO SAMPLES MY FRIEND")\n                    return\n                }\n\n                let heartbeatSeriesQuery = HKHeartbeatSeriesQuery(heartbeatSeries: sample) {\n                    (query, timeSinceSeriesStart, precededByGap, done, error) in\n\n                    guard error == nil else {\n                        print("error in HKHeartbeatSeriesQuery: \\(String(describing: error))")\n                        return\n                    }\n\n                    print("timeSinceSeriesStart = \\(timeSinceSeriesStart), precededByGap = \\(precededByGap)")\n\n                }\n\n                self.healthStore.execute(heartbeatSeriesQuery)\n            }\n\n            completionHandler()\n\n            self.healthStore.execute(heartbeatSeriesSampleQuery)\n        }\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
samples: [count=41 5D4F0C84-294D-41A7-8F64-B387A8E767A3 "Vishal\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (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\xe2\x80\x99s Apple\xc2\xa0Watch" (6.1.1), "Watch3,2" (6.1.1)"Apple Watch"  (2020-01-18 12:12:06 -0800 - 2020-01-18 12:13:12 -0800)\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n
timeSinceSeriesStart = 1.1875, precededByGap = false\ntimeSinceSeriesStart = 2.046875, precededByGap = false\ntimeSinceSeriesStart = 2.92578125, precededByGap = false\ntimeSinceSeriesStart = 3.8125, precededByGap = false\n...\n
Run Code Online (Sandbox Code Playgroud)\n\n

我是否错误地使用了 API?或者这不是获取实时心跳数据的可行方法?

\n

All*_*lan 5

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