Write data

The following examples show you how to write data as part of a common workflow.


The following code example shows how to write basic data inserting step counts:

suspend fun insertSteps(healthConnectClient: HealthConnectClient) {
    val records = listOf(
            count = 120,
            startTime = START_TIME,
            endTime = END_TIME,
            startZoneOffset = START_ZONE_OFFSET,
            endZoneOffset = END_ZONE_OFFSET,
    val response = healthConnectClient.insertRecords(records)

Inserting data: Sample rates

When inserting data into Health Connect, consider using appropriate sample rates to help reduce storage load. For example, it's worth thinking about how frequently step count data needs to be recorded, or what kind of sample rate data types linked to an active workout like speed should require.

Not every data type requires the same sample rate. This is because there may be little added benefit to users having step count data updated every second as opposed to every 60 seconds, for example. However, smaller sample rates may give users a more detailed and granular look at their health and fitness data. Balance detail and performance by adjusting sample rate frequencies.


The Nutrition data type in Health Connect is vast and comprehensive. It includes a wide variety of optional nutrient fields ranging from total carbs to vitamin D and vitamin E. Each data point represents the nutrients that were potentially consumed as part of a meal or food item.

The following code example shows how to insert nutrition data for a user who’s eaten a banana:

val banana = Nutrition(
    name = "banana",
    kcal = 105.0,
    dietaryFiberGrams = 3.1,
    potassiumGrams = 0.422 ,
    totalCarbohydrateGrams = 27.0,
    totalFatGrams = 0.4,
    saturatedFatGrams = 0.1,
    sodiumGrams = 0.001,
    sugarGrams = 14.0,
    vitaminB6Grams = 0.0005,
    vitaminCGrams = 0.0103,
    startTime = START_TIME,
    endTime = END_TIME,
    startZoneOffset = null,
    endZoneOffset = null,

// Insert nutrition data
suspend fun writeNutritionData(food: Nutrition, client: HealthConnectClient) {
    val foodRecords = listOf(food)

Series data

The following code example shows how to write heart rate series data to Health Connect.

//Insert heart rate series data
suspend fun writeHeartRateSeries(client: HealthConnectClient){
 val records = listOf(food)
     startTime = START_TIME,
     startZoneOffset = null,
     endTime = END_TIME,
     endZoneOffset = null,
    //record 10 measurements
     samples =
     List(10) { index ->
         time = START_TIME + Duration.ofSeconds(index.toLong()),
         beatsPerMinute = 100 + index.toLong(),