Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store ECG data on apple healthkit?

I am new to IOS app development. I have been trying to learn how to work with Apple HealthKit API. So far, as an experiment I have managed to build a simple app which could store and retrieve data from the HealthKit such as blood type, heart rate etc (i can furnish the code if anyone needs it-it is already available on the internet). I am able to do this functionality because healthkitStore exposes these typeIdentifiers for the app developers. However, I am a bit lost when I want to create a new typeIdentifier such for storing ECG/EKG on the healthKit? I want to feed ECG/EKG signals into my app and use the HealthKitStore to save these information. Am i missing something?I know I am slow, but i have searched a lot over the internet, but I could not find any specific solutions. Is this not possible? But the whole point of opening the API to the developers is to create new apps with different features. I have no specific requirement as far as storing and retrieving ECG data is concerned, as i simply want to create a PoC without any constraints but focusing on the functionality.

Will I be wrong If i want to create the above by using

struct HKClinicalTypeIdentifier

and then use Clinical Record type identifier

static let labResultRecord: HKClinicalTypeIdentifier

Is this the correct direction? Any direction, motivation or criticism is much welcomed.


1 Answers

In iOS 14 you can read ECG data using new API

HKElectrocardiogramQuery Apple Documentation

here is the sample code I used to retrieve ECG data:

if #available(iOS 14.0, *) {
        let predicate = HKQuery.predicateForSamples(withStart: Date.distantPast,end: Date.distantFuture,options: .strictEndDate)
        let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
        let ecgQuery = HKSampleQuery(sampleType: HKObjectType.electrocardiogramType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor]){ (query, samples, error) in
            guard let samples = samples,
                let mostRecentSample = samples.first as? HKElectrocardiogram else {
                return
            }
            print(mostRecentSample)
            var ecgSamples = [(Double,Double)] ()
            let query = HKElectrocardiogramQuery(mostRecentSample) { (query, result) in
                
                switch result {
                case .error(let error):
                    print("error: ", error)
                    
                case .measurement(let value):
                    print("value: ", value)
                    let sample = (value.quantity(for: .appleWatchSimilarToLeadI)!.doubleValue(for: HKUnit.volt()) , value.timeSinceSampleStart)
                    ecgSamples.append(sample)
                    
                case .done:
                    print("done")
                }
            }
            self.healthMonitor.healthStore.execute(query)
        }
        
        
        healthMonitor.healthStore.execute(ecgQuery)
    } else {
        // Fallback on earlier versions
    }
like image 53
mkhoshpour Avatar answered Oct 22 '25 02:10

mkhoshpour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!