我正在将苹果健康工具包与我的一个应用程序集成,我想知道如何在苹果健康工具包中获得用于生成心电图读数的算法版本。为了获得心电图读数,我使用以下代码
private func readECG(){
let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast,end: Date.distantFuture,options: [])
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let sampleQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: [sortDescriptor]){ (query, samples, error) in
guard let samples = samples,
let mostRecentSample = samples.first as? HKElectrocardiogram else {
return
}
print(getReadableECGClassification(classification: mostRecentSample.classification))
print(mostRecentSample.averageHeartRate!)
print(mostRecentSample.sourceRevision.source.bundleIdentifier)
print(mostRecentSample.device?.model ?? "No device")
print(mostRecentSample.startDate)
print(mostRecentSample.endDate)
print(mostRecentSample.samplingFrequency!)
print(mostRecentSample.uuid)
var ecgSamples = [(Double,Double)] ()
let query = HKElectrocardiogramQuery(mostRecentSample) { (query, result) in
switch result {
case .error(let error):
print("ECG error: ", error)
case .measurement(let value):
let sample = (value.quantity(for: .appleWatchSimilarToLeadI)!.doubleValue(for: HKUnit.volt()) , value.timeSinceSampleStart)
ecgSamples.append(sample)
case .done:
print("ECG : (ecgSamples)")
@unknown default:
print("ECG not found")
}
}
self.healthStore.execute(query)
}
healthStore.execute(sampleQuery)
}
private func getReadableECGClassification(classification: HKElectrocardiogram.Classification?) -> String{
var ecgType = "Not Retrived"
if classification != nil {
switch classification!.rawValue{
case 0:
ecgType = "Not set"
case 1:
ecgType = "Sinus rhythm"
case 2:
ecgType = "Atrial fibrillation"
case 3:
ecgType = "Inconclusive Low Heart Rate"
case 4:
ecgType = "Inconclusive High Heart Rate"
case 5:
ecgType = "Inconclusive Poor Reading"
case 6:
ecgType = "Inconclusive Other"
case 100:
ecgType = "Unrecognized"
default:
ecgType = ""
}
}
return ecgType
}
我发现HKAppleECGAlgorithmVersion
和HKMetadataKeyAppleECGAlgorithmVersion
在那里,不知道如何从apple helath工具包中读取。
它应该在ECG样本的元数据属性上。查找HKAppleECGAlgorithVersion密钥。