Get started

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

Refer to the following resources before you begin installation of Health Connect.

Resources

  • Health Connect SDK (available on Jetpack). Include this SDK in your application to use the Health Connect API.
  • API reference. See the Jetpack reference for the Health Connect API.
  • API Request Developer Declaration Form. Form to request access to read and/or write data types.
  • Github code sample and codelab (Optional). See the Github code sample repository and the codelab to help you get started.

Follow these quick and simple steps to integrate with Health Connect:

Step 0: Install Health Connect

Install Health Connect by Android from the Google Play Store to your device. It will handle all requests sent by your application using the Health Connect SDK.

Step 1: Add the Health Connect SDK to your application

Add a dependency on the Health Connect SDK in your module’s build.gradle file:

dependencies {
  ...
  implementation "androidx.health.connect:connect-client:1.0.0-alpha10"
  ...
}

Find the latest version on release.

Step 2: Get Health Connect client

HealthConnectClient is an entry point to the Health Connect API.

Here’s a step-by-step description of the process of connecting with Health Connect:

  1. To interact with Health Connect within the app, declare the Health Connect package name in your AndroidManifest.xml file:

    <!-- Check whether Health Connect is installed or not -->
    <queries>
        <package android:name="com.google.android.apps.healthdata" />
    </queries>
    
  2. Check if Health Connect is installed. If it is, obtain a HealthConnectClient instance.

    if (HealthConnectClient.isProviderAvailable(context)) {
      // Health Connect is available and installed.
      val healthConnectClient = HealthConnectClient.getOrCreate(context)
    } else {
      // ...
    }
    
  3. The client app must then request permission from the user. The user can grant or deny access to the client app at any point.

HealthConnectClient automatically manages its connection to the underlying storage layer and handles all IPC and serialization of outgoing requests and incoming responses.

Step 3: Declare permissions

  1. Declare the permissions for the data types that your app will use.

    Every data type your app reads or writes needs to be declared using a permission in your manifest. For the full list of permissions and their corresponding data types, see List of data types.

    Starting SDK alpha10 and above, Health Connect uses the standard Android permission declaration format.

    <uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
    <uses-permission android:name="android.permission.health.WRITE_HEART_RATE"/>
    <uses-permission android:name="android.permission.health.READ_STEPS"/>
    <uses-permission android:name="android.permission.health.WRITE_STEPS"/>
    

    If you are using sdk version alpha09 or below, create an array resource in res/values/health_permissions.xml.

    <resources>
      <array name="health_permissions">
        <item>androidx.health.permission.HeartRate.READ</item>
        <item>androidx.health.permission.HeartRate.WRITE</item>
        <item>androidx.health.permission.Steps.READ</item>
        <item>androidx.health.permission.Steps.WRITE</item>
      </array>
    </resources>
    
  2. Add metadata to your manifest to explain your app's use of Health Connect.

    You manifest needs to provide <activity> which displays to the user a privacy policy describing how the user's data is used and handled. You can either add a new <activity>, or modify an existing one.

    This activity needs to:

    1. (For alpha09 and below only) Reference the health_permissions resource mentioned above; and

    2. Declare that it handles the ACTION_SHOW_PERMISSIONS_RATIONALE intent. This intent is sent to the app when the user clicks on the "Read privacy policy" link in the Health Connect permissions dialog.

    <application>
        ...
        <!-- Activity to show rationale of Health Connect permissions -->
        <activity
            android:name=".PermissionsRationaleActivity"
            android:exported="true"
            android:enabled="true">
          <!-- (a) For alpha09 and below, reference permissions resource -->
          <meta-data
              android:name="health_permissions"
              android:resource="@array/health_permissions" />
          <!-- (b) handle intent -->
          <intent-filter>
            <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
          </intent-filter>
        </activity>
        ...
    </application>
    

Step 4: Request permissions from the user

To display the permission dialog, first build a set of permissions for required data types. Ensure the permissions in the set are first declared, as previously described.

For alpha10 and above:

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

For alpha09 and below:

// build a set of permissions for required data types
val PERMISSIONS =
 setOf(
   HealthPermission.createReadPermission(HeartRateRecord::class),
   HealthPermission.createWritePermission(HeartRateRecord::class),
   HealthPermission.createReadPermission(StepsRecord::class),
   HealthPermission.createWritePermission(StepsRecord::class)
 )

Next, use getGrantedPermissions() to see if your app already has the required permissions. If not, use the createRequestPermissionResultContract() method to request permission.

// 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(PERMISSIONS)
   if (granted.containsAll(PERMISSIONS)) {
     // Permissions already granted, proceed with inserting or reading data.
   } else {
     requestPermissions.launch(PERMISSIONS)
   }
}

If the user declines your permission request twice, your app is permanently locked out, and cannot request permissions again. If this happens to your app during development and testing, reset the permission request count by opening the Health Connect app and selecting allow all then remove all. Alternatively use adb to clear the cache of the Health Connect package.

Users can grant or revoke permissions using the Health Connect app at any time. Don't assume that the set of granted permissions does not change; your app should periodically check for granted permissions, and be able to handle scenarios where permission is unexpectedly lost.

Health data is sensitive, so to maintain user trust, only request permission to access data in context. Do no request data that is not used by your app.