How to get daily sleep duration using Apple HealthKit?
Asked Answered
P

2

6

I'm doing an app that reads daily steps and sleep data from Apple HealthKit.

For Steps, it's pretty easy because it is a HKQuantityType, so I can apply HKStatisticsOptionCumulativeSum option on it. Put the start date, end date, and date interval in, and you got it.

- (void)readDailyStepsSince:(NSDate *)date completion:(void (^)(NSArray *results, NSError *error))completion {
    NSDate *today = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:date];
    comps.hour = 0;
    comps.minute = 0;
    comps.second = 0;

    NSDate *midnightOfStartDate = [calendar dateFromComponents:comps];
    NSDate *anchorDate = midnightOfStartDate;

    HKQuantityType *stepType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
    HKStatisticsOptions sumOptions = HKStatisticsOptionCumulativeSum;
    NSPredicate *dateRangePred = [HKQuery predicateForSamplesWithStartDate:midnightOfStartDate endDate:today options:HKQueryOptionNone];

    NSDateComponents *interval = [[NSDateComponents alloc] init];
    interval.day = 1;
    HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:stepType quantitySamplePredicate:dateRangePred options:sumOptions anchorDate:anchorDate intervalComponents:interval];

    query.initialResultsHandler = ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *result, NSError *error) {

        NSMutableArray *output = [NSMutableArray array];

        // we want "populated" statistics only, so we use result.statistics to iterate
        for (HKStatistics *sample in result.statistics) {
            double steps = [sample.sumQuantity doubleValueForUnit:[HKUnit countUnit]];
            NSDictionary *dict = @{@"date": sample.startDate, @"steps": @(steps)};
            //NSLog(@"[STEP] date:%@ steps:%.0f", s.startDate, steps);
            [output addObject:dict];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion != nil) {
                NSLog(@"[STEP] %@", output);
                completion(output, error);
            }
        });
    };

    [self.healthStore executeQuery:query];
}

But for Sleep it's not so straight forward. There are many things I stuck on.

  • First, unlike steps, sleep is a HKCategoryType. So we cannot use HKStatisticsCollectionQuery to sum it because this method only accepts HKQuantityType.
  • Also there are 2 value types of sleep, HKCategoryValueSleepAnalysisInBed and HKCategoryValueSleepAnalysisAsleep. I'm not sure which value is best for just the sleep duration. I'll just use HKCategoryValueSleepAnalysisAsleep only for simplicity.
  • Sleep data comes in an array of HKCategorySample objects. Each with start date and end date. How do I effectively combine those data, trim it to within a day, and get the daily sleep duration (in minutes) out of it? I found this DTTimePeriodCollection class in DateTool pod that may do this job, but I haven't figure it out yet.

Simply put, if anyone knows how to get daily sleep duration using Apple HealthKit, please tell me!

Pelletier answered 8/6, 2015 at 5:56 Comment(2)
hi, have you find any solution?Orchestrate
Have you find any solution for sleep hours?Dalenedalenna
B
4

Check how I have did this, its working for me to get collection of the sleep data

func sleepTime() {
        let healthStore = HKHealthStore()
        // startDate and endDate are NSDate objects
        // first, we define the object type we want
        if let sleepType = HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis) {
            // You may want to use a predicate to filter the data... startDate and endDate are NSDate objects corresponding to the time range that you want to retrieve
            //let predicate = HKQuery.predicateForSamplesWithStartDate(startDate,endDate: endDate ,options: .None)
            // Get the recent data first
            let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
            // the block completion to execute
            let query = HKSampleQuery(sampleType: sleepType, predicate: nil, limit: 100000, sortDescriptors: [sortDescriptor]) { (query, tmpResult, error) -> Void in
                if error != nil {
                    // Handle the error in your app gracefully
                    return
                }
                if let result = tmpResult {
                   for item in result {
                        if let sample = item as? HKCategorySample {
                               let startDate = sample.startDate
                               let endDate = sample.endDate
                               print()
                             let sleepTimeForOneDay = sample.endDate.timeIntervalSince(sample.startDate)
                        }
                    }
                }
          }
    }

This gives the array of entry slots.

Boyne answered 13/9, 2019 at 14:54 Comment(0)
H
0

I used this:

@import HealthKit;

@implementation HKHealthStore (AAPLExtensions)


- (void)hkQueryExecute:(void (^)(double, NSError *))completion {
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDate *now = [NSDate date];

NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];

NSDate *startDate = [calendar dateFromComponents:components];

NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

HKSampleType *sampleType = [HKSampleType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionNone];

HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:0 sortDescriptors:nil resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
    if (!results) {
        NSLog(@"An error occured fetching the user's sleep duration. In your app, try to handle this gracefully. The error was: %@.", error);
        completion(0, error);
        abort();
    }

        double minutesSleepAggr = 0;
        for (HKCategorySample *sample in results) {

            NSTimeInterval distanceBetweenDates = [sample.endDate timeIntervalSinceDate:sample.startDate];
            double minutesInAnHour = 60;
            double minutesBetweenDates = distanceBetweenDates / minutesInAnHour;

            minutesSleepAggr += minutesBetweenDates;
        }
        completion(minutesSleepAggr, error);
}];

[self executeQuery:query];
}

And then in view controller:

- (void)updateUsersSleepLabel {
[self.healthStore hkQueryExecute: ^(double minutes, NSError *error) {
    if (minutes == 0) {
        NSLog(@"Either an error occured fetching the user's sleep information or none has been stored yet.");

        dispatch_async(dispatch_get_main_queue(), ^{
            self.sleepDurationValueLabel.text = NSLocalizedString(@"Not available", nil);
        });
    }
    else {

        int hours = (int)minutes / 60;
        int minutesNew = (int)minutes - (hours*60);
        NSLog(@"hours slept: %ld:%ld", (long)hours, (long)minutesNew);

        dispatch_async(dispatch_get_main_queue(), ^{
            self.sleepDurationValueLabel.text = [NSString stringWithFormat:@"%d:%d", hours, minutesNew] ;
        });
    }


}];
}
Hospitable answered 17/6, 2015 at 15:23 Comment(2)
I don't think that's correct. Your results is an array of sleep chunks that you have to aggregate/trim to within a specified date. In your code, the completion block can get called multiple times with no aggregation.Pelletier
In your HKSampleQuery completion block should you be filtering on HKCategoryValuesSleepAnalysis.Asleep so you count the asleep time only? I think with what you have now you are also counting the InBed time.Viscounty

© 2022 - 2024 — McMap. All rights reserved.