gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:19:34 75 4
gpt4 key购买 nike

我正在尝试实时访问从 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,与我的活跃锻炼 session 不对应——无论我是开始 HKWorkoutSession,还是在我的 watch 上手动进行锻炼,这似乎都没有改变。

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

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

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

最佳答案

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

关于ios - 使用 HKHeartbeatSeriesSample 从 Apple Watch 获取实时心跳数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833094/

75 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com