首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查询实时心率变异性和心率值

查询实时心率变异性和心率值
EN

Stack Overflow用户
提问于 2020-05-28 16:06:35
回答 1查看 331关注 0票数 0

我正在试着从苹果手表中读出实时心率+心率变异性数据。我可以读取HRV值,但不知道如何实现heartRate的读取。有没有可能同时把它们读出来?

代码语言:javascript
运行
复制
class HealthKitManager {

    private var healthStore = HKHealthStore()
    private var heartRateQuantity = HKUnit(from: "count/min")
    private var heartRateVariability = HKUnit(from: "count/min")
    private var activeQueries = [HKQuery]()

    @Published var heartRateValues = HeartRateValues()

    func autorizeHealthKit() {

        let heartRate = HKObjectType.quantityType(forIdentifier: .heartRate)!
        let heartRateVariability = HKObjectType.quantityType(forIdentifier: .heartRateVariabilitySDNN)!

        let HKreadTypes: Set = [heartRate, heartRateVariability]

        healthStore.requestAuthorization(toShare: nil, read: HKreadTypes) { (success, error) in
            if let error = error {
                print("Error requesting health kit authorization: \(error)")
            }
        }
    }

    func fetchHeartRateData(quantityTypeIdentifier: HKQuantityTypeIdentifier ) {

        let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
        let updateHandler: (HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, Error?) -> Void = {
        query, samples, deletedObjects, queryAnchor, error in
            guard let samples = samples as? [HKQuantitySample] else {
                return
            }
            self.process(samples, type: quantityTypeIdentifier)
        }
        let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!, predicate: devicePredicate, anchor: nil, limit: HKObjectQueryNoLimit, resultsHandler: updateHandler)
        query.updateHandler = updateHandler
        healthStore.execute(query)
        activeQueries.append(query)
    }

    private func process(_ samples: [HKQuantitySample], type: HKQuantityTypeIdentifier) {
        for sample in samples {
            if type == .heartRate {
                DispatchQueue.main.async {
                    self.heartRateValues.heartRate = sample.quantity.doubleValue(for: self.heartRateQuantity)
                }
            } 
                // Not Sure about this part and readings show 0.0
                else if type == .heartRateVariabilitySDNN {
                DispatchQueue.main.async {
                    self.heartRateValues.heartRateVariability = sample.quantity.doubleValue(for: self.heartRateVariability)
                }
            }
        }
    }

    func stopFetchingHeartRateData() {
        activeQueries.forEach { healthStore.stop($0) }
        activeQueries.removeAll()
        DispatchQueue.main.async {
            self.heartRateValues.heartRate = 0.0
            self.heartRateValues.heartRateVariability = 0.0
        }

    }

}

这里提出了一个类似的问题,但没有得到答案:Get Apple watch heartRateVariabilitySDNN realtime?

EN

回答 1

Stack Overflow用户

发布于 2020-05-28 18:51:21

我也设法获得了小时数,只是补充道:

代码语言:javascript
运行
复制
private func process(_ samples: [HKQuantitySample], type: HKQuantityTypeIdentifier) {
    for sample in samples {
        if type == .heartRate {
            DispatchQueue.main.async {
                self.heartRateValues.heartRate = sample.quantity.doubleValue(for: self.heartRateQuantity)
            }
        } else if type == .heartRateVariabilitySDNN {
            DispatchQueue.main.async {
                self.heartRateValues.heartRateVariability = sample.quantity.doubleValue(for: self.heartRateVariabilityQuantity)
            }
        }
    }
}

然后使用以下命令调用该类:

代码语言:javascript
运行
复制
    fetchHeartRateData(quantityTypeIdentifier: .heartRate)
    fetchHeartRateData(quantityTypeIdentifier: .heartRateVariabilitySDNN)

问题仍然是get的HRV值读取一次,并且不会随着时间的推移而改变。有什么想法吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62059881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档