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
}
}
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.