iOS:Healthkit实时获取血氧饱和度

iOS:Healthkit实时获取血氧饱和度,ios,swift,healthkit,health-monitoring,Ios,Swift,Healthkit,Health Monitoring,我是HealthKit框架的新手,希望在血流中实时显示血氧饱和度 REF: 从这份报告中,我试图询问血氧饱和度 func createOxygenSaturationStreamingQuery(_ workoutStartDate: Date) -> HKQuery? { guard let quantityType = HKObjectType.quantityType(forIdentifier: .oxygenSaturation) else { retur

我是HealthKit框架的新手,希望在血流中实时显示血氧饱和度

REF:

从这份报告中,我试图询问血氧饱和度

   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:我正在办理登机手续

我从回购中得到了心率,但氧气水平是空的吗


让我知道我做错了什么,如何纠正?

watchOS不会自动记录血氧饱和度样本,因此,除非您有一台设备正在记录这种类型的样本并使用应用程序将其写入HealthKit,否则您不应该期望返回任何结果。

您确定要获得氧气水平的许可吗?是的,我已经添加了许可,任何具体的??我已在授权时添加了阅读权限是的,您已将其添加到正确的位置。但要确保它需要VO2max@MoazKhan:您可以添加代码吗,这将对我有更多帮助:)所以我要做的是获得结果??安装一个记录血氧饱和度的应用程序。是否有任何iOS库可用于通过摄像头测量血氧水平?目前,查询每5秒更新一次。我们能把时间间隔改为10秒或1分钟吗?用心率计算血氧有什么线索吗@阿披舍克塔普里亚尔