【发布时间】:2018-04-26 07:34:29
【问题描述】:
我是 HealthKit 框架的新手,想要实时血流中的氧饱和度。
参考: https://github.com/coolioxlr/watchOS-2-heartrate
从这个仓库我试图查询氧饱和度
func createOxygenSaturationStreamingQuery(_ workoutStartDate: Date) -> HKQuery? {
guard let quantityType = HKObjectType.quantityType(forIdentifier: .oxygenSaturation) else { return nil }
let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate, end: nil, options: .strictEndDate )
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate])
let oxygenSaturationQuery = HKAnchoredObjectQuery(type: quantityType,
predicate: predicate,
anchor: nil,
limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -> Void in
self.updateOxygenSaturation(sampleObjects)
}
oxygenSaturationQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) -> Void in
self.updateOxygenSaturation(samples)
}
return oxygenSaturationQuery
}
func updateOxygenSaturation(_ samples: [HKSample]?) {
guard let oxygenSaturationSamples = samples as? [HKQuantitySample] else {return}
DispatchQueue.main.async {
// RETURN FROM HERE AS EMPTY ARRAY
guard let sample = oxygenSaturationSamples.first else { return }
let value = sample.quantity.doubleValue(for: HKUnit(from: "%/min"))
let stringValue = String(UInt16(value))
self.label.setText(stringValue)
// retrieve source from sample
let name = sample.sourceRevision.source.name
print("sample.sourceRevision.source.name : \(name)")
print("PERCENT : \(stringValue)")
}
}
P.S:我正在模拟器中检查
我从那个 repo 获得心率,但氧气水平是空的?
让我知道我做错了什么以及如何改正?
【问题讨论】:
-
您确定要获得氧气水平的许可吗?
-
是的,我已经添加了权限,任何具体的??
-
我在授权时添加了读取权限
-
是的,您已将其添加到正确的位置。但请确保它要求 VO2 max
-
@MoazKhan : 你也可以添加代码吗,它会帮助我更多:)
标签: ios swift healthkit health-monitoring