I need to be able to create and store a HeartbeatSeries for a given time-period from an Apple Watch, to then retrieve that data from HealthKit to be processed.
I have working code which allows me to begin a workout session, which is being used to determine how long a session has been running for. I also have working code for retrieving HeartbeatSeries data from HealthKit. The issue is that no HeartbeatSeries data is being stored into HealthKit as a result of the workout session running. Whether that session is running for as little as 30 seconds or as long as 20 minutes, nothing is stored.
However, when I use the the Apple "Meditation" app (formerly known as "Breathe"), I can query HealthKit afterwards and retrieve a list of individual heartbeat timings during that 2 minute period. Therefore, it IS possible to store a HeartbeatSeries from within an app on the Apple Watch.
What I would like to know is, how can I use the pulse sensor built-in to the Apple Watch to be able to record a HeartbeatSeries similar to how the Meditation app does it.
There is no API that provides direct access to the pulse sensor built-in to the Apple Watch, but with HealthKit, you should be able to retrieve the heartbeat series in the following way:
-
Use the start date, end date, and sample type (
HKSeriesType.heartbeat()
) to query the heartbeat series samples (HKHeartbeatSeriesSample) in the HealthKit store. -
Use HKHeartbeatSeriesQuery to access the underlying beat-to-beat measurements for a
HKHeartbeatSeriesSample
sample.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.