Track mindfulness

This guide is compatible with Health Connect version 1.1.0-alpha11.

Health Connect provides a mindfulness data type to measure various aspects of mental health, such as stress and anxiety. Mindfulness is a data type that is part of overall wellness in Health Connect.

Required permissions

As with any data type in Health Connect, access to a mindfulness session is protected by a pair of permissions: READ_MINDFULNESS_SESSION and WRITE_MINDFULNESS_SESSION.

Request permissions from the user

After creating a client instance, your app needs to request permissions from the user. Users must be allowed to grant or deny permissions at any time.

To do so, create a set of permissions for the required data types. Make sure that the permissions in the set are declared in your Android manifest first.

// Create a set of permissions for required data types
val PERMISSIONS =
    setOf(
  HealthPermission.getReadPermission(MindfulnessSessionRecord::class),
  HealthPermission.getWritePermission(MindfulnessSessionRecord::class)
)

Use getGrantedPermissions to see if your app already has the required permissions granted. If not, use createRequestPermissionResultContract to request those permissions. This displays the Health Connect permissions screen.

// Create the permissions launcher
val requestPermissionActivityContract = PermissionController.createRequestPermissionResultContract()

val requestPermissions = registerForActivityResult(requestPermissionActivityContract) { granted ->
  if (granted.containsAll(PERMISSIONS)) {
    // Permissions successfully granted
  } else {
    // Lack of required permissions
  }
}

suspend fun checkPermissionsAndRun(healthConnectClient: HealthConnectClient) {
  val granted = healthConnectClient.permissionController.getGrantedPermissions()
  if (granted.containsAll(PERMISSIONS)) {
    // Permissions already granted; proceed with inserting or reading data
  } else {
    requestPermissions.launch(PERMISSIONS)
  }
}

Because users can grant or revoke permissions at any time, your app needs to periodically check for granted permissions and handle scenarios where permission is lost.

Information included in a mindfulness session record

Each mindfulness session record captures any type of mindfulness session a user performs, for example meditation, breathing, and movement. The record can also include additional notes about the session.

Supported aggregations

Health Connect lets you get the following aggregate values for a given list of deltas:

Example usage

The following code snippet shows how to write a meditation mindfulness session using the Jetpack library:

if (healthConnectClient.features.getFeatureStatus(FEATURE_MINDFULNESS_SESSION) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {
        healthConnectClient.insertRecords(listOf(MindfulnessSessionRecord(
            startTime = Instant.now().minus(Duration.ofHours(1)),
            startZoneOffset = ZoneOffset.UTC,
            endTime = Instant.now(),
            endZoneOffset = ZoneOffset.UTC,
            mindfulnessSessionType = MindfulnessSessionRecord.MINDFULNESS_SESSION_TYPE_MEDITATION,
            title = "Lake meditation",
            notes = "Meditation by the lake",
            metadata = Metadata.activelyRecorded(
                clientRecordId = "myid",
                clientRecordVersion = 0.0,
                device = Device(type = Device.TYPE_PHONE)
            ),
        )))
    }