Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HKworkoutsession always an active workout session

I'm working on apple watch app and I'm using HKworkoutsession to access heart rate data sample.

In the newest watchos2 beta3 release bug "During an active workout session, new heart rate samples are not generated when the screen is off." is fixed.

My question is how could I set my HKworkoutsession always as an "active work out session" thus I could keep getting heart rate sample as I need.

Thanks Ryan

like image 620
Nevermore Avatar asked Jul 14 '15 17:07

Nevermore


People also ask

What is workoutsession and how to use it?

WorkoutSession is used to store data related to the current PrancerciseWorkout being tracked. Since you added in this concept of workout intervals to PrancerciseWorkout, WorkoutSession needs to add new intervals each time you start and end a Prancercise session.

How do I use hkworkoutbuilder with HealthKit?

The first thing you do is create a health store and workout configuration to store data into HealthKit. Next, add the following to the end of the method: Use the HKWorkoutBuilder class (new to iOS 12) to incrementally construct a workout and begin collecting workout data.

What is a a workout?

A workout is a container for these types of information, taken as a collection of samples. A given workout might contain heart rate samples, distance samples and an activity type to categorize them. Continuing from the previous HealthKit tutorial, you’re going to track a special kind of workout: Prancercise.

What is hkquery predicateforworkouts?

HKQuery.predicateForWorkouts (with:) is a special method that gives you a predicate for workouts with a certain activity type. In this case, you’re loading any type of workout in which the activity type is other (all Prancercise workouts use the other activity type).


1 Answers

Code following is how to start or stop workout session.

let healthStore = HKHealthStore()
healthStore.startWorkoutSession(workoutSession) {
  (result: Bool, error: NSError?) -> Void in
}

healthStore.stopWorkoutSession(workoutSession) {
  (result: Bool, error: NSError?) -> Void in
}

There is HKWorkoutSessionDelegate which notifies its session state.

protocol HKWorkoutSessionDelegate : NSObjectProtocol {

func workoutSession(workoutSession: HKWorkoutSession,
  didChangeToState toState: HKWorkoutSessionState,
  fromState: HKWorkoutSessionState, date: NSDate)

func workoutSession(workoutSession: HKWorkoutSession,
  didFailWithError error: NSError)
}

[Edited] 2015/08/31

ObjC version

HKWorkoutSession *workoutSession = [[HKWorkoutSession alloc] initWithActivityType:HKWorkoutActivityTypeRunning locationType:HKWorkoutSessionLocationTypeOutdoor];
workoutSession.delegate = self;

HKHealthStore *healthStore = [HKHealthStore new];
[healthStore startWorkoutSession:workoutSession];

[healthStore stopWorkoutSession:workoutSession];

HKWrokoutSessionDelegate

- (void)workoutSession:(HKWorkoutSession *)workoutSession
  didChangeToState:(HKWorkoutSessionState)toState
         fromState:(HKWorkoutSessionState)fromState
              date:(NSDate *)date;

- (void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error;

Caution: There is a change of method name with the latest version, watch OS 2 beta 5.

stopWorkoutSession has changed to endWorkoutSession.

like image 105
Yusuke Kita Avatar answered Oct 05 '22 23:10

Yusuke Kita