Get started

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

Resources

  • Client SDK (available on Jetpack). Include this SDK in your application to use the Health Connect API.
  • Health Connect APK (available on Play Store). Install the APK from Play Store. It will handle all requests sent by your application using the Health Connect SDK.
  • API reference. See the Jetpack reference for the Health Connect API.
  • Github code samples (Optional). See the Github code sample repository to help you get started.

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

Step 1:

First, make sure you do the following:

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

dependencies {
   …
  implementation 'androidx.health:health-connect-client:1.0.0-alpha01'
}

Install the Health Connect APK to your device:

First, opt-in to the Health Connect open beta testing program here. The open beta link also shows you how to download the Health Connect app. Once you opt-in, install the Health Connect APK on your device.

Step 2:

Declare the below Activity in your AndroidManifest.xml to handle intent that will explain your app's use of permissions.

<application …>
    <activity name=".PermissionsRationaleActivity" exported="true">
        <intent-filter>
            <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE"/>
        </intent-filter>
        <meta-data android:name="health_permissions"
                android:resource="@array/health_permissions" />
    </activity>

Step 3: Permissions

Declare the permissions your app will use. Create an array resource in res/values/health_permissions.xml. Note that you will need to add a line for every permission your app will use:

<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>

To start the request permissions dialogue within your application, first build a set of permissions for required data types. Make sure that the permissions in the set are also declared in res/values/health_permissions.xml.

// build a set of permissions for required data types
val PERMISSIONS =
 setOf(
   Permission.createReadPermission(HeartRateSeries::class),
   Permission.createWritePermission(HeartRateSeries::class),
   Permission.createReadPermission(Steps::class),
   Permission.createWritePermission(Steps::class)
 )

Then, request permissions. Check if permissions have already been granted before launching the permissions request.

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

fun checkPermissionsAndRun(client: HealthConnectClient) {
 lifecycleScope.launch {
   val granted = client.permissionController.getGrantedPermissions(PERMISSIONS)
   if (granted.containsAll(PERMISSIONS)) {
     // Permissions already granted
   } else {
     requestPermissions.launch(PERMISSIONS)
   }
 }
}

Step 5: Connecting to the Health Connect API

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. Update AndroidManifest.xml.

    // Add this to AndroidManifest.xml
    <!-- To check whether healthcore apk is installed or not -->
    <queries>
        <package android:name="com.google.android.apps.healthdata" />
    </queries>
    
  2. In the MainActivity, check if Health Connect is installed. If it is, obtain a HealthConnectClient instance.

    if (HealthConnectClient.isAvailable(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 outcoming requests and incoming responses.

At this point, you can begin performing CRUD operations!