Accessing heart rate sensors

How do I approach using the raw data from the heart rate sensors? I'm aware that in WatchOS2 we will be able to do this and from what I understand the new Xcode beta includes everything we need to get started. However, I can't find any documentation. Do we access these sensors via HealthKit? How do we call the sensors?

Replies

apparently no one knows, or is not willing to share.


People will point to HealthKit, but they obviously haven't looked into the documentation.


I've successfully created an HKWorkoutSession which i believe is key to getting this done, but Apple refuses (i have contacted them several times) to release the source code for their "Whats new in HealthKit" session at this years WWDC.


If anyone has a code example, please provide. Otherwise, please stop pointing towards healthkit. You're not giving specific answers because you yourself don't know how to do it.

I have answered this question here on StackOverflow. Here is a rehash of what it says


Apple isn't technically giving developers access to the heart rate sensors in watchOS 2.0. What they are doing is providing direct access to hear rate data recorded by the sensor in HealthKit. To do this and get data in near-real time, there are two main things you need to do: tell the watch that you are starting a workout and start a streaming query from healthkit.


// Create a new workout session

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

self.workoutSession!.delegate = self;


// Start the workout session

self.healthStore.startWorkoutSession(self.workoutSession!)


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


// This is the type you want updates on. It can be any health kit type, including heart rate.

let distanceType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)

// Match samples with a start date after the workout start

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)

}

// This is called each time a new value is entered into HealthKit (samples may be batched together for efficiency)

distanceQuery.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in

// Handle update notifications after the query has initially run

// TODO: do whatever you want with samples (note you are not on the main thread)

}


// Start the query

self.healthStore.executeQuery(distanceQuery)


This is all described in full detail in the demo at the end of the video What's New in HealthKit - WWDC 2015

We're creating a medical app which requires us to measure the heart rate in near realtime for a few minutes when we need it.


Are you telling me that there's no way right now to do that without starting a Workout? It just doesn't seem right that a medical app should start a Workout for measuring the heart rate. (for example, what HKWorkoutActivityType would it have?)

That's exactly what he's saying. Third party apps do not have direct access to the heart rate sensor.

Hi. On watchOS 2, you will be able to start high-rate sampling of the heart rate sensor by starting a workout session. When you are done with your measurements, you will stop the workout session. At that time you can chose if you will save the workout into the HealthKit data store, or not. If you just need a few heart rate data points, then you might chose not to save the workout.


You can test this now, even if you don't have a watch set up with watchOS 2 beta, and don't have an iPhone with iOS 9 beta. Here is what you need to do. Get a third party heart rate sensor that is compatible with HealthKit. You can write a HealthKit app on iOS 8 for the iPhone to test your ideas and to prototype your user interactions, although you'd use this third party sensor, and an iOS 8 iPhone. Then, when you have access to a watchOS 2 device and an iOS 9 iPhone, much of the HealthKit specific code you wrote can be used unchanged in your WatchKit 2 app.


The sample from our 2015 WWDC session on HealthKit is not available, but as you are watching the video, you should be able to follow along in Xcode. In the end, you will have a sample app that works.


-zk

Hi ,


Can i get API for heart rate sensors and pulse oximeter ? Actually i'm very worried about my senior project if it can be done or not i will be very grateful to you !!!


Thanks.

You can use the "Report Bugs" link at the bottom of the page to request changes or enhancements to the public API.

Hi, I am having a proper functioning app, whose only problem is the follwing:
Heart rate should temporarily be measured and not stored, but the health app reflects my session with increasing the activity rings, but I don't store the session. I understand it's essential to have a workout session, but how can this happen when I don't save, propper drop the workout session with "healthStore.end" and set the session to nil?

Besides this, I ask for reading permissions only … So my app is not allowed to store the data either.


Thanks for any idea…

Have there been any developments since this forum post?


There are many applications that could use real-time heart rate data.


Many thanks