Forum Xamarin.iOS

How to READ/WRITE Blood glucose and ECG datas from Healthkit

HelmutLubikHelmutLubik DEUniversity ✭✭

Hi,

does anyone have an example on how to read/write blood glucose and ECG datas from HealthKit ?

Thanks in advance
Helmut

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    It seems we still can't get Electrocardiogram from health kit API:
    https://forums.developer.apple.com/thread/108532
    You could try heart rate.
    Here is the code about reading glucose:

    void readSample()
    {
    
        HKQuantityType glucoseType = HKQuantityType.Create(HKQuantityTypeIdentifier.BloodGlucose);
        var SortDescriptor = new NSSortDescriptor(HKSample.SortIdentifierStartDate, false);
        var sampleQuery = new HKSampleQuery(glucoseType, null, 1, new NSSortDescriptor[] { SortDescriptor }, (query, correlations, error) =>
        {
            foreach (HKCorrelation correlation in correlations)
            {
                HKQuantitySample glucoseSample = (HKQuantitySample)correlation.GetObjects(glucoseType).AnyObject;
                HKQuantity glucoseQuantity = glucoseSample.Quantity;
    
                double mmol = glucoseQuantity.GetDoubleValue(HKUnit.CreateMoleUnit(HKUnit.MolarMassBloodGlucose));
            }
        });
    
        healthStore.ExecuteQuery(sampleQuery);
    }
    

    Write code:

    void writeSample()
    {
        HKQuantityType glucoseType = HKQuantityType.Create(HKQuantityTypeIdentifier.BloodGlucose);
        HKQuantity glucoseQuantity = HKQuantity.FromQuantity(HKUnit.CreateMoleUnit(HKUnit.MolarMassBloodGlucose), glucoseValue);
        var sample = HKQuantitySample.FromType(glucoseType, glucoseQuantity, startDate, endDate);
    
        healthStore.SaveObject(sample, (success, error) =>
        {
        });
    }
    
  • HelmutLubikHelmutLubik DEUniversity ✭✭

    Hi @LandLu ,

    thanks for the code. But it doesn't work.

    Your writeSample-code returned this error:

    Objective-C exception thrown. Name: _HKObjectValidationFailureException Reason: HKQuantitySample 185 mol<180.1558800000541> (2019-05-01 16:38:32 +0200 - 2019-05-01 16:38:32 +0200) requires unit of type Mass/Volume. Incompatible unit: mol<180.1558800000541>
    Native stack trace:
    0 CoreFoundation 0x0000000111b396fb __exceptionPreprocess + 331
    1 libobjc.A.dylib 0x0000000112abfac5 objc_exception_throw + 48
    2 CoreFoundation 0x0000000111b39555 +[NSException raise:format:] + 197
    3 HealthKit 0x000000010bab8358 -[HKObject _validateForCreation] + 115
    4 HealthKit 0x000000010bab81b3 +[HKObject _newDataObjectWithMetadata:device:config:] + 240
    5 HealthKit 0x000000010bab807f +[HKSample _newSampleWithType:startDate:endDate:device:metadata:config:] + 213
    6 HealthKit 0x000000010babb70f +[HKQuantitySample quantitySampleWithType:quantity:startDate:endDate:device:metadata:] + 298
    7 HealthKit 0x000000010babd519 +[HKQuantitySample quantitySampleWithType:quantity:startDate:endDate:] + 99
    8 Test.iOS 0x00000001042b3ec9 xamarin_dyn_objc_msgSend + 217
    9 ??? 0x000000013bdb1e5b 0x0 + 5299183195

    and with the readSample, I get no values. correlations is an empty List.

    regards
    Helmut

  • HelmutLubikHelmutLubik DEUniversity ✭✭

    @LandLu :) Great, thanks.

    Some more questions.

    1. How do I read/write blood glucose values in mg/dl too ?
    2. How do I read/write the preferredUnits for the given quantity types ?

    regards
    Helmut

  • LandLuLandLu Member, Xamarin Team Xamurai

    How do I read/write blood glucose values in mg/dl too ?

    The unit for mg/dL is

    HKUnit.FromGramUnit(HKMetricPrefix.Milli).UnitDividedBy(HKUnit.CreateLiterUnit(HKMetricPrefix.Deci))
    

    Apply this unit in HKQuantity.FromQuantity method to write mg/dL values. So does glucoseQuantity.GetDoubleValue.

    How do I read/write the preferredUnits for the given quantity types ?

    Change the HKUnit as you want. But please notice you have to use the corresponding unit for quantity types or we will get the incompatible error just like you saw before.

Sign In or Register to comment.