ygsankar
ygsankar

Reputation: 119

How to read iOS Healthkit Blood Pressure (systolic,diastolic) using HKObserverquery?

By using the following code i can read heart rate data automatically using observer query, when data is available in healthkit.

HKSampleType *readGlucoseType;

        readGlucoseType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];


    query = [[HKObserverQuery alloc]initWithSampleType:readGlucoseType
                                             predicate:nil
                                         updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
             {

                 if (!error)
                 {
                     [self handleHKQueryResponse:query completionHandler:completionHandler errorCode:error];
                 }
                 else
                 {
                     NSLog(@"Observerquery Error");

                     if (completionHandler)
                     {
                         completionHandler();
                     }
                 }
             }];

    [self.healthStore executeQuery:query];

How do i need to read the blood pressure data from health kit using observer query?

Upvotes: 2

Views: 4307

Answers (3)

Drmorgan
Drmorgan

Reputation: 539

Updated the answer for Xcode 9.2:

func readSampleByBloodPressure()
    {
         guard let type = HKQuantityType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.bloodPressure),
        let systolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic),
        let diastolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic) else {

            return
    }

    let sampleQuery = HKSampleQuery(sampleType: type, predicate: nil, limit: 0, sortDescriptors: nil) { (sampleQuery, results, error) in
        if let dataList = results as? [HKCorrelation] {
            for data in dataList
            {
                if let data1 = data.objects(for: systolicType).first as? HKQuantitySample,
                    let data2 = data.objects(for: diastolicType).first as? HKQuantitySample {

                    let value1 = data1.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
                    let value2 = data2.quantity.doubleValue(for: HKUnit.millimeterOfMercury())

                    print("\(value1) / \(value2)")
                }
            }
        }
    }
    healthStore.execute(sampleQuery)
    }

Upvotes: 10

Zeezer
Zeezer

Reputation: 1533

Chances are that you came here looking for a solution how to read blood pressure from healthkit in swift. With that said, here is my solution in swift.

func readSampleByBloodPressure()
{

    let past = NSDate.distantPast() as! NSDate
    let now   = NSDate()
    let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: true)
    let type = HKQuantityType.correlationTypeForIdentifier(HKCorrelationTypeIdentifierBloodPressure)
    let sampleQuery = HKSampleQuery(sampleType: type, predicate: nil, limit: 0, sortDescriptors: [sortDescriptor])
        { (sampleQuery, results, error ) -> Void in

            let dataLst = results as? [HKCorrelation];

            for var index=0;index<dataLst!.count;++index
            {

                let data1 = (dataLst![index].objectsForType(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureSystolic))).first as? HKQuantitySample
                let data2 = dataLst![index].objectsForType(HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodPressureDiastolic)).first as? HKQuantitySample

                if let value1 = data1!.quantity?.doubleValueForUnit(HKUnit.millimeterOfMercuryUnit()) , value2 = data2!.quantity?.doubleValueForUnit(HKUnit.millimeterOfMercuryUnit()) {
                    println(value1)
                    println(value2)
                }
            }

    }
    self.healthKitStore.executeQuery(sampleQuery)       

}

Upvotes: 3

Bhanu Prakash
Bhanu Prakash

Reputation: 1483

Here is the sample code for getting Blood pressure data from health kit.

    HKCorrelationType *correlationType = [HKCorrelationType correlationTypeForIdentifier:HKCorrelationTypeIdentifierBloodPressure];

[self.healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];

HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
        if (!error)
             {
                 [self handleHKQueryResponse:query completionHandler:completionHandler errorCode:error];
             }
             else
             {
                 NSLog(@"Observerquery Error");

                 if (completionHandler)
                 {
                     completionHandler();
                 }
             }
    [self.healthStore executeQuery:query];

Upvotes: -1

Related Questions