I'm new to HealthKit framework and wants real time oxygen saturation in blood stream.
REF : https://github.com/coolioxlr/watchOS-2-heartrate
FROM THIS REPO i have tried to make query for oxygen saturation
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)")
}
}
PS : I'M CHECKING IN SIMULATOR
I'm getting Heart Rate from that repo, but getting oxygen level empty?
Let me know what wrong i have done and how can i correct?
watchOS不会自动记录氧饱和度样本,因此除非您拥有一台可以记录此类样本并使用应用将其写入HealthKit的设备,否则您不应期望获得任何结果。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.