问题描述
-
我正在使用下面的函数来获取heartRateVariabilitySDNN,但是它只能获取一次,并且不能像listenbeat一样实时计算?
I am using below function to get heartRateVariabilitySDNN, but its get only once and not calculate realtime like hearbeat does?
func HRVstart(){守护让数量类型= HKObjectType.quantityType(forIdentifier:.heartRateVariabilitySDNN)否则{return}
func HRVstart() { guard let quantityType = HKObjectType.quantityType(forIdentifier: .heartRateVariabilitySDNN) else { return }
self.healthStore.execute(self.HRVStreamingQuery())
// Create query to receive continiuous heart rate samples.
let datePredicate = HKQuery.predicateForSamples(withStart: Date(), end: nil, options: .strictStartDate)
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
let queryPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate, devicePredicate])
let updateHandler: HKQueryUpdateHandler = { [weak self] query, samples, deletedObjects, queryAnchor, error in
if let quantitySamples = samples as? [HKQuantitySample] {
self?.HRVprocess(samples: quantitySamples)
}
}
let query = HKAnchoredObjectQuery(type: quantityType,
predicate: queryPredicate,
anchor: nil,
limit: HKObjectQueryNoLimit,
resultsHandler: updateHandler)
query.updateHandler = updateHandler
// Execute the heart rate query.
healthStore.execute(query)
// Remember all active Queries to stop them later.
self.HRVactiveQueries.append(query)
}
private func HRVStreamingQuery() -> HKQuery {
let predicate = HKQuery.predicateForSamples(withStart: NSDate() as Date, end: nil, options: .strictStartDate)
let query = HKAnchoredObjectQuery(type: self.heartRateVaribalityType, predicate: nil, anchor: nil, limit: Int(HKObjectQueryNoLimit)) {
(query, samples, deletedObjects, anchor, error) -> Void in
self.HRVformatSamples(samples: samples)
}
query.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in
self.HRVformatSamples(samples: samples)
}
self.HRVactiveQueries.append(query)
return query
}
private func HRVformatSamples(samples: [HKSample]?) {
guard let samples = samples as? [HKQuantitySample] else { return }
guard let quantity = samples.last?.quantity else { return }
let beats = quantity.doubleValue(for: HKUnit.secondUnit(with: .milli))
let timestamp = samples[0].endDate
let newHeartRateVariablity = HeartRateVariablity(timestamp: timestamp, hrv: beats)
delegate?.heartRateVariablity(didChangeTo: newHeartRateVariablity)
print("HeartRateVariablity: \(beats)")
}
推荐答案
根据我的经验,HRV仅由watchOS在特定时间测量.锻炼活动似乎不是其中之一.使用呼吸"应用似乎触发了HRV测量.
From my experience, HRV is only measured at specific times by watchOS. A workout activity does not seem to be one of those times. Using the 'Breathe' app seems to trigger a HRV measurement.
这不是一个很好的解决方案,但是您可以要求用户在活动会话前后进行测量之前和之后使用呼吸"应用程序.但是我不知道在会议期间获取当前HRV编号的任何方法.
It's not a great solution, but you can ask your users to use the 'Breathe' app before and after the activity session for before and after measurements. But I don't know of any way to get current HRV numbers during the session.
我希望其他人有更好的解决方案!
I hope someone else has a better solution!
这篇关于获取Apple Watch heartRateVariabilitySDNN实时信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!