Read raw data

The following example shows you how to read raw data as part of the common workflow.

Simple read

The Steps data type in Health Connect captures the number of steps a user has taken between readings. Step counts represent a common measurement across health, fitness, and wellness platforms. Health Connect makes it simple to read and write step count data.

To read records, create a ReadRecordsRequest and supply it when you call readRecords.

The following example shows how to read step count data for a user within a certain time. For an extended example with SensorManager, see the step count data guide.

suspend fun readStepsByTimeRange(
    healthConnectClient: HealthConnectClient,
    startTime: Instant,
    endTime: Instant
) {
    try {
        val response = healthConnectClient.readRecords(
            ReadRecordsRequest(
                StepsRecord::class,
                timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
            )
        )
        for (stepRecord in response.records) {
            // Process each step record
        }
    } catch (e: Exception) {
        // Run error handling here
    }
}

Read previously written data

If an app has written records to Health Connect before, it is possible for that app to read them back without requiring a Read permission to those specific records. This is applicable to scenarios in which the app needs to resync with Health Connect after the user has reinstalled it.

To read data in this scenario, you need to indicate the package name as a DataOrigin object in the dataOriginFilter parameter of your ReadRecordsRequest.

The following example shows how to indicate a package name when reading Steps records:

try {
    val response =  healthConnectClient.readRecords(
        ReadRecordsRequest(
            recordType = StepsRecord::class,
            timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
            dataOriginFilter = setOf(DataOrigin("com.my.package.name"))
        )
    )
    for (record in response.records) {
        // Process each record
    }
} catch (e: Exception) {
    // Run error handling here
}

30-day read restriction

Health Connect can read data for up to 30 days prior to when any permission was first granted.

However, if a user deletes your app, the permission history is lost. If the user reinstalls your app and grants permission again, your app can read data from Health Connect up to 30 days prior to that new date.

Example

If a user first granted read permission to your application on March 30, 2023, the earliest data your app could read back would be from February 28, 2023 onwards.

The user then deletes your app on May 10, 2023. The user decides to reinstall it on May 15, 2023 and grant read permission. The earliest date your app can now read data from is April 15, 2023.

Foreground restriction

Reading data with Health Connect is restricted to applications running in the foreground. This restriction is in place to further strengthen user privacy. This assures users that Health Connect does not have background read access to their data, and that data is only read and accessed in the foreground.

For situations in which interruptions are tolerable, such as displaying a reading in your application, read directly from Health Connect to your client application.

For situations in which you prefer no interruptions, such as reading a range of data from Health Connect then writing and uploading it elsewhere, use a ForegroundService, rather than an Activity where it can be quickly dismissed.