Aggregate data request

You can retrieve a data’s total or minimum and maximum values or the last data entry by using the function:

  • HealthDataStore.aggregateData()

Total steps

To get total steps, use DataType.StepsType.TOTAL.
To obtain the aggregated total steps for each hour of the day, call the following function:

  • setLocalTimeFilterWithGroup()

It takes two parameters - LocalTimeFilter and LocalTimeGroup. If the LocalTimeFilter is set from the beginning of today to the current time and the LocalTimeGroup of an hour interval is used, the result dataList will contain aggregated total steps for every hour of the day.

You can also sum of all elements of the dataList and verify that the resulting total matches the step count for the entire day as displayed in the Samsung Health Pedometer tracker.

private suspend fun readStepCount(startTime: LocalDateTime, endTime: LocalDateTime) {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    val localtimeFilter = LocalTimeFilter.of(startTime, endTime)

    val readRequest = DataType.StepsType.TOTAL.requestBuilder
        .setLocalTimeFilterWithGroup(
            localtimeFilter,
            LocalTimeGroup.of(LocalTimeGroupUnit.HOURLY, 1))
        .setOrdering(Ordering.ASC)
        .build()

    val dataList = healthDataStore.aggregateData(readRequest).dataList
    dataList.forEach {
        val hourlyStepCount = it.value
    }
    val dailyStepCount = dataList.sumOf { it.value as Long }
}

Minimum and maximum heart rate values

You can retrieve minimum and maximum heart rate data from a specific time range using the following example:

suspend fun minHeartRateAggregateRequest(startDate: LocalDate, endDate : LocalDate) {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    val localDateFilter = LocalDateFilter.of(startDate, endDate)
    val minHeartRateAggregate = DataType.HeartRateType.MIN.requestBuilder
        .setLocalDateFilter(localDateFilter)
        .build()

    val dataList = healthDataStore.aggregateData(minHeartRateAggregate).dataList
    dataList.firstOrNull().let {
        val minHR = it?.value
    }
}

suspend fun maxHeartRateAggregateRequest(startDate: LocalDate, endDate : LocalDate) {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    val localDateFilter = LocalDateFilter.of(startDate, endDate)
    val maxHeartRateAggregate = DataType.HeartRateType.MAX.requestBuilder
        .setLocalDateFilter(localDateFilter)
        .build()

    val dataList = healthDataStore.aggregateData(maxHeartRateAggregate).dataList
    dataList.firstOrNull().let {
        val maxHR = it?.value
    }
}

Last bedtime of sleep goal data

The aggregate operation LAST refers to the most recent values that have been saved or updated in Samsung Health.
To get the last bedtime goal, see the example below. This value is the same as that in Samsung Health > Sleep Tracker > Set target.

suspend fun lastBedTimeAggregateRequest(startDate: LocalDate, endDate: LocalDate) {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    val localDateFilter = LocalDateFilter.of(startDate, endDate)
    val lastBedTimeAggregate = DataType.SleepGoalType.LAST_BED_TIME.requestBuilder
        .setLocalDateFilter(localDateFilter)
        .build()

    val dataList = healthDataStore.aggregateData(lastBedTimeAggregate).dataList
    dataList.lastOrNull().let {
        val lastBedTime = it?.value
    }
}