0

I am working on my first ResearchKit project. I am trying to get heart rate data through apple's HealthKit. I am testing the program on my phone, and I have the apple watch with Health data, it should be available. The walking task starts and finishes successfully, and I am able to parse through the result files. But I am finding that the result files only contain physical sensor data (accelerometer and gyro) and NOT any health data.

What concerns me a little is that I see these 2 warning on the console output when the walking task starts:

ORKSample[511:80256] [ResearchKit][Warning] __82-[ORKTaskViewController requestHealthStoreAccessWithReadTypes:writeTypes:handler:]_block_invoke Health access: error=(null)
2016-04-07 16:31:28.097 ORKSample[511:80630] [ResearchKit][Warning] __59-[ORKTaskViewController requestPedometerAccessWithHandler:]_block_invoke Pedometer access: error=(null)

It seems that _block_invole Health access and _block_invoke Pedometer access can't be good.

Here is code that I use to authorize the health data:

import ResearchKit
import HealthKit

class HealthDataStep: ORKInstructionStep {
    // MARK: Properties

    let healthDataItemsToRead: Set<HKObjectType> = [
        HKObjectType.characteristicTypeForIdentifier(HKCharacteristicTypeIdentifierDateOfBirth)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeight)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
    ]

    let healthDataItemsToWrite: Set<HKSampleType> = [
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBodyMass)!,
        HKObjectType.workoutType(),
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!,
        HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)!
    ]

    // MARK: Initialization

    override init(identifier: String) {
        super.init(identifier: identifier)

        title = NSLocalizedString("Health Data", comment: "")
        text = NSLocalizedString("On the next screen, you will be prompted to grant access to read and write some of your general and health information, such as height, weight, and steps taken so you don't have to enter it again.", comment: "")
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: Convenience

    func getHealthAuthorization(completion: (success: Bool, error: NSError?) -> Void) {
        guard HKHealthStore.isHealthDataAvailable() else {
            let error = NSError(domain: "com.example.apple-samplecode.ORKSample", code: 2, userInfo: [NSLocalizedDescriptionKey: "Health data is not available on this device."])

            completion(success: false, error:error)

            return
        }

        // Get authorization to access the data
        HKHealthStore().requestAuthorizationToShareTypes(healthDataItemsToWrite, readTypes: healthDataItemsToRead) { (success, error) -> Void in
            completion(success:success, error:error)
        }
    }
}

To implement the walking task I use this code, very simple:

public var TimedWalkTask: ORKOrderedTask {
    return ORKOrderedTask.fitnessCheckTaskWithIdentifier("WalkTask",intendedUseDescription: nil,walkDuration: 15 as NSTimeInterval, restDuration: 15 as NSTimeInterval,options: .None)
}

Just wondering if anyone knows if I am missing something. The program seems to run correctly and return results, but the results don't contain that health data that I'm looking for.

Just for your information, I have attached a screen shot of the health permissions for my app in the iphone settings: enter image description here

jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160
  • 1
    Apple watch doesn't monitor heart rate all the time, you need to pair your phone with a BTLE heart rate monitor instead. – Yuan Apr 07 '16 at 16:34

1 Answers1

1

Yuan is correct -- samples are not synced to the phone from Apple Watch in real time, so you won't pick them up in your active task. You might still be able to do it in your study, but you'd need to use a historical HealthKit query to grab samples relevant to previous tasks, and relate them later when processing the data.

Also, the unless there is a workout session active on the Apple Watch, heart rate samples will only be collected infrequently. If you want higher frequency heart rate samples, you'll need a Watch app that starts a workout session as part of your study, or you'll need to ask users to start a Workout with the Workout app on the Watch for the duration of the task.

jwe
  • 436
  • 2
  • 4