如何解决WatchOS 检测身体是否存在心率
在 watchOS 中,有没有办法通过检测身体是否存在来确定是否显示心率?
我发现当我取下手表时,心率传感器不断返回值。我想在手表取下时显示“-”。
下面是我用来获取心率的函数。
谢谢。
private func heartRateQuery(quantityTypeIdentifier: HKQuantityTypeIdentifier) {
// We want data points from our current device
let devicePredicate = HKQuery.predicateForObjects(from: [HKDevice.local()])
// A query that returns changes to the HealthKit store,including a snapshot of new changes and continuous monitoring as a long-running query.
let updateHandler: (HKAnchoredObjectQuery,[HKSample]?,[HKDeletedObject]?,HKQueryAnchor?,Error?) -> Void = {
query,samples,deletedObjects,queryAnchor,error in
// A sample that represents a quantity,including the value and the units.
guard let samples = samples as? [HKQuantitySample] else {
return
}
self.process(samples,type: quantityTypeIdentifier)
}
// Provides with both the ability to receive a snapshot of data,and then on subsequent calls,a snapshot of what has changed.
let query = HKAnchoredObjectQuery(type: HKObjectType.quantityType(forIdentifier: quantityTypeIdentifier)!,predicate: devicePredicate,anchor: nil,limit: HKObjectQueryNoLimit,resultsHandler: updateHandler)
query.updateHandler = updateHandler
// query execution
healthStore.execute(query)
}
private func processHeartRate(_ samples: [HKQuantitySample],type: HKQuantityTypeIdentifier) {
// Variable initialization
var lastHeartRate = 0.0
// Get the first sample value
guard let sample = samples.first else{
print("No samples!")
return
}
// Get current heart rate
if type == .heartRate {
lastHeartRate = sample.quantity.doubleValue(for: heartRateQuantity)
self.value = Int(lastHeartRate)
hrLabel.setText(String(value))
print("The lastHeartRate is \(lastHeartRate)")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。