Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heart Rate data on apple Watch

Can we access the heart rate directly from the apple watch? I know this is a duplicate question, but no one has asked this in like 5 months. I know you can access it from the Health App but I'm not sure how "real-time" that will be.

like image 736
Andrew Garcia Avatar asked Mar 04 '15 15:03

Andrew Garcia


People also ask

Can I see my heart rate history on Apple Watch?

1. Press the Digital Crown and open the Heart Rate app, which looks like a heart on a red background. 2. When the app starts, you can see the most recent heart rate reading, which should have been taken in the last 10 minutes.

How do you use heart rate data?

To use your heart rate to monitor your exercise intensity, you first need to calculate your maximum heart rate. This can be estimated by subtracting your age from the number 220. For example, someone who is 30 years old would calculate his or her maximum heart rate as follows: 220 – 30 = 190 bpm.

Where is heart rate data on iPhone?

Open the Health app on your iPhone. Tap the Browse tab in the bottom right corner, then Heart. On the main page, you'll see the various heart rate categories. Tap one to see your history.


2 Answers

After exploring HealthKit and WatchKit Extension, My findings are as follows:

  1. We do not need the WatchKit Extension to get the Heart Rate Data.

  2. You just need to have an iPhone with paired Apple watch (which is obvious)

  3. The Default Apple Watch Heart Rate monitor app updates the HealthKit data immediately only when it is in the foreground.

  4. When the Default Apple Watch Heart Rate monitor app is in the Background, it updates the HealthKit data at the interval of 9-10 mins.

  5. To get the Heart rate data from the HealthKit following query needs to be fired periodically.

    func getSamples() {
    
        let heathStore = HKHealthStore()
    
        let heartrate = HKQuantityType.quantityType(forIdentifier: .heartRate)
        let sort: [NSSortDescriptor] = [
            .init(key: HKSampleSortIdentifierStartDate, ascending: false)
        ]
    
        let sampleQuery = HKSampleQuery(sampleType: heartrate!, predicate: nil, limit: 1, sortDescriptors: sort, resultsHandler: resultsHandler)
    
        heathStore.execute(sampleQuery)
    
    }
    
    func resultsHandler(query: HKSampleQuery, results: [HKSample]?, error: Error?) {
    
        guard error == nil else {
            print("cant read heartRate data", error!)
            return
        }
    
        guard let sample = results?.first as? HKQuantitySample else { return }
    
        // let heartRateUnit: HKUnit = .init(from: "count/min")
        // let doubleValue = sample.quantity.doubleValue(for: heartRateUnit)
    
        print("heart rate is", sample)
    }
    

Please update me if anyone gets more information.
Happy Coding.

Update

I've updated your code to be clear and general, and be aware that you need to get authorization for reading HeathKit data and adding info.plist key Privacy - Health Records Usage Description

like image 25
shoan Avatar answered Sep 22 '22 13:09

shoan


Heart Rate Raw Data information is now available in Watchkit for watchOS 2.0.

WatchOS 2 includes many enhancements to other existing frameworks such as HealthKit, enabling access to the health sensors that access heart rate and health information in real-time.

You could check this information in the following session which is total 30 minutes presentation.If you do not want to watch entire session, then you directly jump to Healthkit API features which is in between 25-28 min:

WatchKit for watchOS 2.0 Session in WWDC 2015

Here is the source code implementation link

As stated in the HKWorkout Class Reference:

The HKWorkout class is a concrete subclass of the HKSample class. HealthKit uses workouts to track a wide range of activities. The workout object not only stores summary information about the activity (for example, duration, total distance, and total energy burned), it also acts as a container for other samples. You can associate any number of samples with a workout. In this way, you can add detailed information relevant to the workout.

In that given link, the following part of the code defines sample rate of heartRate

NSMutableArray *samples = [NSMutableArray array];

HKQuantity *heartRateForInterval =
[HKQuantity quantityWithUnit:[HKUnit unitFromString:@"count/min"]
                 doubleValue:95.0];

HKQuantitySample *heartRateForIntervalSample =
[HKQuantitySample quantitySampleWithType:heartRateType
                                quantity:heartRateForInterval
                               startDate:intervals[0]
                                 endDate:intervals[1]];

[samples addObject:heartRateForIntervalSample];

As they state there:

You need to fine tune the exact length of your associated samples based on the type of workout and the needs of your app. Using 5 minute intervals minimizes the amount of memory needed to store the workout , while still providing a general sense of the change in intensity over the course of a long workout. Using 5 second intervals provides a much-more detailed view of the workout, but requires considerably more memory and processing.

like image 61
casillas Avatar answered Sep 20 '22 13:09

casillas