Write data

Stay organized with collections Save and categorize content based on your preferences.

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

Steps

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

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

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.

Nutrition

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:

suspend fun insertNutrition(healthConnectClient: HealthConnectClient) {
    val banana =
        NutritionRecord(
            name = "banana",
            energy = 105.0.kilocalories,
            dietaryFiber = 3.1.grams,
            potassium = 0.422.grams,
            totalCarbohydrate = 27.0.grams,
            totalFat = 0.4.grams,
            saturatedFat = 0.1.grams,
            sodium = 0.001.grams,
            sugar = 14.0.grams,
            vitaminB6 = 0.0005.grams,
            vitaminC = 0.0103.grams,
            startTime = START_TIME,
            endTime = END_TIME,
            startZoneOffset = START_ZONE_OFFSET,
            endZoneOffset = END_ZONE_OFFSET,
        )
    healthConnectClient.insertRecords(listOf(banana))
}

Series data

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

suspend fun insertHeartRateSeries(healthConnectClient: HealthConnectClient) {
    val heartRateRecord =
        HeartRateRecord(
            startTime = START_TIME,
            startZoneOffset = START_ZONE_OFFSET,
            endTime = END_TIME,
            endZoneOffset = END_ZONE_OFFSET,
            // records 10 arbitrary data, to replace with actual data
            samples =
                List(10) { index ->
                    HeartRateRecord.Sample(
                        time = START_TIME + Duration.ofSeconds(index.toLong()),
                        beatsPerMinute = 100 + index.toLong(),
                    )
                },
        )
    healthConnectClient.insertRecords(listOf(heartRateRecord))
}