How to read algorithm version used to generate an ECG reading in apple health kit?

140 Views Asked by At

I am integrating apple healthkit with one of my app and I wonder how to get the algorithm version used to generate an ECG reading in apple health kit.To get ECG reading I use following code

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
}

I found that HKAppleECGAlgorithmVersion and HKMetadataKeyAppleECGAlgorithmVersion are there and don't know how to read it from the apple helath kit.

1

There are 1 best solutions below

1
On

It should be on the metadata property of the ECG sample. Look for HKAppleECGAlgorithmVersion key.