Track beat to beat heart rate apple watch through HRV
Asked Answered
D

3

7

I'm looking into ways of getting the beat-to-beat intervals of separate heartbeat intervals.

I'm wearing an Apple Watch and am currently able to export

<Record type=“HKQuantityTypeIdentifierHeartRateVariabilitySDNN” sourceName=“Apple Watch” sourceVersion=“4.0” device=“&lt;&lt;HKDevice: 0x1c489da10&gt;, name:Apple Watch, manufacturer:Apple, model:Watch, hardware:Watch3,4, software:4.0&gt;” unit=“ms” creationDate=“2017-10-31 13:41:54 +0000" startDate=“2017-10-31 13:40:43 +0000” endDate=“2017-10-31 13:41:54 +0000" value=“92.7156”>
  <HeartRateVariabilityMetadataList>
   <InstantaneousBeatsPerMinute bpm=“69" time=“13:40:45.22”/>
   <InstantaneousBeatsPerMinute bpm=“74" time=“13:40:46.02”/>
   <InstantaneousBeatsPerMinute bpm=“71" time=“13:40:46.87”/>
   <InstantaneousBeatsPerMinute bpm=“64" time=“13:40:47.81”/>
   <InstantaneousBeatsPerMinute bpm=“54" time=“13:40:48.92”/>
   <InstantaneousBeatsPerMinute bpm=“57" time=“13:40:49.97”/>
   <InstantaneousBeatsPerMinute bpm=“65" time=“13:40:50.90”/>
   <InstantaneousBeatsPerMinute bpm=“63" time=“13:40:51.86”/>
   <InstantaneousBeatsPerMinute bpm=“60" time=“13:40:52.86”/>
   <InstantaneousBeatsPerMinute bpm=“57" time=“13:40:53.92”/>
   <InstantaneousBeatsPerMinute bpm=“57" time=“13:40:54.98”/>
   <InstantaneousBeatsPerMinute bpm=“61" time=“13:40:55.97”/>
   <InstantaneousBeatsPerMinute bpm=“70" time=“13:40:56.83”/>
   <InstantaneousBeatsPerMinute bpm=“72" time=“13:40:57.66”/>
   <InstantaneousBeatsPerMinute bpm=“69" time=“13:40:58.54”/>
   <InstantaneousBeatsPerMinute bpm=“58" time=“13:40:59.58”/>
   <InstantaneousBeatsPerMinute bpm=“58" time=“13:41:02.75”/>
   <InstantaneousBeatsPerMinute bpm=“63" time=“13:41:03.70”/>
   <InstantaneousBeatsPerMinute bpm=“70" time=“13:41:04.56”/>
   <InstantaneousBeatsPerMinute bpm=“70" time=“13:41:05.41”/>
   <InstantaneousBeatsPerMinute bpm=“69" time=“13:41:13.15”/>
   <InstantaneousBeatsPerMinute bpm=“71" time=“13:41:13.99”/>
   <InstantaneousBeatsPerMinute bpm=“63" time=“13:41:21.11”/>
   <InstantaneousBeatsPerMinute bpm=“69" time=“13:41:21.98”/>
   <InstantaneousBeatsPerMinute bpm=“73" time=“13:41:22.79”/>
   <InstantaneousBeatsPerMinute bpm=“65" time=“13:41:35.27”/>
   <InstantaneousBeatsPerMinute bpm=“67" time=“13:41:36.17”/>
   <InstantaneousBeatsPerMinute bpm=“71" time=“13:41:37.01”/>
   <InstantaneousBeatsPerMinute bpm=“77" time=“13:41:37.79”/>
   <InstantaneousBeatsPerMinute bpm=“79" time=“13:41:38.55”/>
  </HeartRateVariabilityMetadataList>
 </Record>

However, when I'm getting a sample of the heartRateVariabilitySDNN, I'm getting only 2 values back.

This is the code I'm using to get the sample

  let healthStore = HKHealthStore()

    var typeHeart = HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)
    var startDate = Date() - 7 * 24 * 60 * 60 // start date is a week
    var predicate: NSPredicate? = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: HKQueryOptions.strictEndDate)

    let sampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRateVariabilitySDNN)

    let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)

    let sampleQuery = HKSampleQuery(sampleType: sampleType!, predicate: predicate, limit: 30, sortDescriptors: [sortDescriptor])
    { (sampleQuery, results, error ) -> Void in
        if(error == nil) {
            print(results)
        }
    }

This all that it's outputting:

Optional([92.3536 ms D7627860-F87C-4316-9943-522BC1D6734B "Apple Watch" (4.0), "Watch3,4" (4.0)"Apple Watch"  (2017-10-31 13:48:19 +0000 - 2017-10-31 13:53:30 +0000), 92.7156 ms EB0DBCB1-164A-4D50-9103-270F3F9FBCD1 "Apple Watch" (4.0), "Watch3,4" (4.0)"Apple Watch"  (2017-10-31 13:40:43 +0000 - 2017-10-31 13:41:54 +0000)])

Which are basically 2 values: 92.7ms and 92.3ms. These are the averages of 2 workouts during the 'breath' application on the Watch.

Does anyone know how I can get to the InstantaneousBeatsPerMinute and/or HRV meta data?

Edit: There seem to be something interesting going on where the outputted value looks very much like a print statement of the Record type in the XML.

Drawbridge answered 31/10, 2017 at 16:56 Comment(4)
did you implemented the HeartRateDelegate?Convoke
you can check in below function if you did func workoutSession(_ workoutSession: HKWorkoutSession, didChangeTo toState: HKWorkoutSessionState, from fromState: HKWorkoutSessionState, date: Date)Convoke
How did you get this XML export? I wonder if there is a way to request HRV, and then fish out the InstantaneousBeatsPerMinute continuously?Boulware
@DavidLowenfels got it through a manual export in the health app. Unfortunately no programmatic way to do this yet...Drawbridge
D
4

After filing an technical support incident (TSI) at Apple about this, they said that it's currently not possible to do this.

Thank you for contacting Apple Developer Technical Support (DTS). Our engineers have reviewed your request and have concluded that there is no supported way to achieve the desired functionality given the currently shipping system configurations.

If you would like for Apple to consider adding support for such features in the future, please submit an enhancement request via the Bug Reporter tool at https://developer.apple.com/bug-reporting/.

If you'd like to see this functionality, please file a bug report!

Drawbridge answered 10/11, 2017 at 22:53 Comment(3)
I also filed a report. This is annoying, I'm making an HRV biofeedback app but apparently not for Apple Watch.Boulware
Got same answer. Apple must add this ability to Health Kit for sure. It is the most valuable information Watch ever had. Let's make Apple know we need it. I've filled report too.Merriweather
Did you have any luck with that on watchOS 5?Immedicable
M
3

In iOS 13 Apple released new Heartbeat Series API. You can find more details here:

https://developer.apple.com/documentation/healthkit/samples/reading_and_writing_healthkit_series_data https://developer.apple.com/documentation/healthkit/hkheartbeatseriessample https://developer.apple.com/documentation/healthkit/hkheartbeatseriesquery

And this related WWDC video session is very useful for understanding this new API: https://developer.apple.com/videos/play/wwdc2019/218

Merriweather answered 26/11, 2019 at 7:23 Comment(0)
M
-4

If you want get Instantaneous heart rate your app should start an HKWorkoutSession.

    self.workoutSession = HKWorkoutSession(activityType: .Running, locationType: .Indoor)
    self.workoutSession!.delegate = self;

    self.healthStore.startWorkoutSession(self.workoutSession!)

Then, you can start a streaming query from HKHealthKit to give you updates as HealthKit receives them:

    let distanceType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
    let predicate = HKQuery.predicateForSamplesWithStartDate(workoutStartDate, endDate: nil, options: .None)
    let distanceQuery = HKAnchoredObjectQuery(type: distanceType!, predicate: predicate, anchor: 0, limit: 0) { (query, samples, deletedObjects, anchor, error) -> Void in
        // Handle when the query first returns results
        // TODO: do whatever you want with samples (note you are not on the main thread)
    }
    distanceQuery.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in
        // Handle update notifications after the query has initially run
    }

    self.healthStore.executeQuery(distanceQuery)
Mangrove answered 7/11, 2017 at 20:34 Comment(5)
My problem is not in getting the heart rate, it's in reading the HRV metadata and writing the HRV.Drawbridge
Please show me how! :) I'd love to give you 200 rep.Drawbridge
@Drawbridge , ok :-) Just try to change HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning) to HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRateVariabilitySDNN)Stopple
@Drawbridge yes. On watchOS 4Stopple
What are you outputting?Drawbridge

© 2022 - 2024 — McMap. All rights reserved.