79507264

Date: 2025-03-13 18:03:01
Score: 0.5
Natty:
Report link

According to the Android documentation, you have to use MindfulnessSessionRecord:

healthConnectClient.insertRecords(
    listOf(
        MindfulnessSessionRecord(
            startTime = startTime,
            endTime = endTime,
            startZoneOffset = ZoneOffset.UTC,
            endZoneOffset = ZoneOffset.UTC,
            mindfulnessSessionType = MINDFULNESS_SESSION_TYPE_MEDITATION,
            metadata = Metadata.manualEntry()
        )
    )
)

Here is a list of the available mindfulnessSessionType values:

enter image description here

If you want to read the entry:

val mindfulnessResponse = healthConnectClient.aggregate(
    AggregateRequest(
        metrics = setOf(MINDFULNESS_DURATION_TOTAL),
        timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
    )
)

val time = mindfulnessResponse[MINDFULNESS_DURATION_TOTAL]?.toMinutes()

Mindfulness is available since 1.1.0-alpha12

androidx-health-connect = { module = "androidx.health.connect:connect-client", version = "1.1.0-alpha12" }

Don't forget the add the required permissions in the manifest

<uses-permission android:name="android.permission.health.READ_MINDFULNESS" />
<uses-permission android:name="android.permission.health.WRITE_MINDFULNESS" />
Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: carlos menjivar