Track mindfulness

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

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.

Feature availability

To determine whether a user's device supports mindfulness data types, check the availability of FEATURE_MINDFULNESS_SESSION on the client:

if (healthConnectClient
     .features
     .getFeatureStatus(
       HealthConnectFeatures.FEATURE_MINDFULNESS_SESSION
     ) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {

  // Feature is available
} else {
  // Feature isn't available
}

See Check for feature availability to learn more.

Required permissions

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

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

There are no supported aggregations for this data type.

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)
            ),
        )))
    }