Read data

You can now retrieve data from Samsung Health using:

  • HealthDataStore.readData()

Apart from the data type, you can also set a time filter in the request.

The example below gives a data point list (representing heart rate values) from the specific time range.

suspend fun readHeartRateData(startTime: LocalDateTime, endTime: LocalDateTime) {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    val localTimeFilter = LocalTimeFilter.of(startTime, endTime)
    val readRequest = DataTypes.HEART_RATE.readDataRequestBuilder
        .setLocalTimeFilter(localTimeFilter)
        .setOrdering(Ordering.DESC)
        .build()

    val heartRateList =
        healthDataStore.readData(readRequest).dataList
    // do data processing
}

The following example demonstrates how to retrieve heart rate data measured by a Galaxy Watch:

val localTimeFilter = LocalTimeFilter.of(startTime, endTime)
val readRequest = DataTypes.HEART_RATE.readDataRequestBuilder
    .setSourceFilter(ReadSourceFilter.fromDeviceType(DeviceGroup.WATCH))
    .setLocalTimeFilter(localTimeFilter)
    .setOrdering(Ordering.ASC)
    .build()

To get data created in the mobile device with Samsung Health installed, set a source filter with a local device.

.setSourceFilter(ReadSourceFilter.fromLocalDevice())