我正在尝试实时访问从Apple Watch记录的心跳数据。通过HKWorkoutSession,我可以每5秒钟获取一次带有心率(心跳/分钟)数据的workoutBuilder didCollectDataOf
回调。
这对于我的应用程序来说不够实时,因此我正在为此目的使用新的(从iOS 13和watchOS 6.0开始)HKHeartbeatSeriesSample(及相关类)。我的理解是,心跳数据的系列样本将以系列形式记录逐次测量。
我能够使API工作并获得一些数据,但不是实时的-我获得的系列数据来自以前的一些录音(不清楚是什么触发了这些录音)。
这里的授权:
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所观察到的更精确地记录心率。 Apple Watch计算心率变异性时,会记录HKSeriesType.heartbeat()
类型的样本(请参阅样本类型heartRateVariabilitySDNN
)。 HRV由Apple Watch在后台定期计算。没有用于启动HRV读数的API。