Empowering Your Health Journey: Android’s Health Connect

Kayvan Kaseb
Software Development
8 min readOct 16, 2023
The picture is provided by Unsplash

As a matter of fact, Google Play Store offers numerous wellness apps for users to achieve their health goals by utilizing various devices like phones, wearables, and at-home health devices. These apps provide valuable data for tracking activity, sleep quality, and weight changes. When combined, this data offers a more comprehensive health picture, and helps users make decisions about workouts, sleep, and goal progress. Users need a unified solution to handle their data, select preferred apps, and ensure privacy. To address the challenges of integrating with various platforms and APIs, Google has developed Health Connect, a universal solution for Android. Health Connect allows users to connect and share data between apps while maintaining data privacy control. It simplifies data management by centralizing it, streamlining integration, and diminishing development costs. This article is focused on discussing Health Connect as a unified platform to manage health data efficiently and securely in Android development.

Introduction

In the Google Play Store, there are countless apps that help people with their wellness goals, such as staying active, getting better sleep, or even tracking their fertility journey. Nowadays, people use a range of devices to keep tabs on their progress. For instance, their phones can count steps and even detect snoring at night. Wearable gadgets can monitor their heart rate and stress levels, and at-home health devices can give accurate readings for things like blood pressure. All these apps and devices provide a ton of valuable information for users. This data is quite useful on its own. It helps users answer straightforward questions like whether they reached their daily activity goals, the quality of their sleep the night before, and how their weight is changing over time.

In fact, when you combine all these data, it offers a clearer picture of your health and wellness. This means it can help you decide if you are up for an intense workout or a simple walk, whether pre-bedtime meditation aids your sleep, and if your fitness and nutrition plan is taking you closer to your goals. Google has considered that users want all their data in one place, and they want the freedom to choose which app they use to see it. Most importantly, users want to ensure their data is private and secure. Besides, Android developers should face with many platforms your apps need to integrate with on android. So, the maintenance could be tricky because each API has a different schema and behavior. This is not just a time-drain; it is also a significant expense for your business. Due to these problematic issues, Google has decided to develop a universal solution, Health Connect, in Android by following this idea: no matter which devices or apps they use.

Health Connect gives you a simple way to store and connect the data between your health and fitness apps, without compromising on privacy. You decide who you want to share your data with and what types of data you want to share.

Health Connect can enable users easily connect and share data between apps. It allows them to handle their health and fitness information from one central place and decide which apps can add data and which ones can access different types of data. Remember, Health Connect is just a solution.

Health Connect

Basically, Health Connect serves as a shared API for storing and exchanging health data on Android devices. Developers can both retrieve and write data into an on-device data store. The schema and API behavior have been standardized to make it easy to use the data. In addition the centralized permissions and privacy controls can make it clear and simple for your users to handle this data and access to it. In other words, it lets users easily manage their health and fitness data from a central location, and decides which apps can add or access specific data types. This unified API is the result of a partnership among Fitbit, Google Fit, and Samsung Health. Now, Health Connect supports more than 50 data types in 6 categories, including: activity, sleep, nutrition, cycle tracking, body measurements, vitals (heart rate and blood pressure).

Health Connect is designed to facilitate fast, convenient integration between client apps and the Health Connect API.

The picture is provided by Google documents

By having user permission, apps can write and read data into Health Connect. Health Connect is designed to give users to provide users with complete privacy control. Users can easily check which data their apps want to access, turn access on or off at any time, delete unwanted data, and manage the priority of these data sources.

For using Health Connect in practice, Health Connect provides an SDK for apps to use the API. They also offer a user app in beta on the Play Store. Essentially, the Health Connect SDK supports Android 8 (API level 26) at the minimum; whereas, the Health Connect app is just only compatible with Android 9 (API level 28) or higher.

Moreover, Health Connect contains a user interface through which apps request a user’s permission to show data. It also offers a list of existing user permissions, which allows users to easily control access to data over multiple Android apps.

Health Connect Functionalities

The platform has basic functions for adding, updating, and removing recorded data. Health Connect also lets client apps sync data out of it. This creates a log of data changes. As a result, this log helps developers and users see if certain data types have been added or removed by other apps.

Aggregation

Clients are able to apply an aggregation function over underlying data. There are a few types of aggregations as follows: Statistical: it provides the average, minimum, or maximum values in a set, like the maximum heart rate during a workout. Cumulative: it provides the sum of values over a given time range, like the total step count in a day. Count: it just offers a simple count of the number of data entries, like number of workouts in a week. Duration: it offers the total duration of one type of data values, like the time spent sleeping this week.

Checking availability

The SDK allows client apps to specify whether the Health Connect API is on the user’s Android device. This means If it is not found, Health Connect triggers an availability check to determine whether the user’s device is suitable.

Checking permissions

Health Connect makes it necessary for you to ask the user’s permission to access their health and fitness data for each app. Also, it lets you check which permissions the user has already approved or declined.

Reading with Health Connect

Only apps running in the foreground can read data through Health Connect. This rule is there to enhance user privacy. Health Connect informs and ensures users that it does not have background access to their data, and can only access data when the app is in the foreground.

Using Health Connect in Android apps

There are some steps for using Health Connect in practice, which have been mentioned briefly as follows:

  1. Adding the Health Connect SDK to your application

You should add the dependency in your module-level build.gradle file:

dependencies {
...
implementation "androidx.health.connect:connect-client:1.1.0-alpha02"
...
}

2. You should mention read and write permissions on the AndroidManifest.xml file based on the needed data types.

3. You should display your app’s privacy policy dialog. This explains the reasons behind the requested permissions and how user data is utilized and protected. This Activity should be set up to handle the “ACTION_SHOW_PERMISSIONS_RATIONALE” intent, which is triggered when users click the privacy policy link on the Health Connect permissions screen.

4. Then, you should obtain a Health Connect client. HealthConnectClient is an entry point to the Health Connect API, and allows the app to use the datastore in the Health Connect app. For having this instance, you should mention the Health Connect package name in your Android manifest first.

<application> ... </application>
...
<queries>
<package android:name="com.google.android.apps.healthdata" />
</queries>

After that in your Activity, you should check if Health Connect is installed using getSdkStatus. If it is, you should get a HealthConnectClient instance.

5. Your app should request permissions from the user, and users must have the freedom to grant or deny these permissions at their discretion. To achieve this, you need to establish a set of permissions for the data types you require. It is essential to make sure that these permissions are declared in your AndroidManifest.xml file beforehand. Furthermore, you can be able to use getGrantedPermissions and createRequestPermissionResultContract to know if your app already has the required permissions granted or not.

6. Finally, you can perform read and write operations in your Android app. For example: the code you provided inserts steps data using the Health Connect API, but it is important to manage exceptions and errors appropriately as shown in the catch block to ensure robust and reliable behavior. Also, It calls the insertRecords method on the healthConnectClient and passes a list containing the stepsRecord as a parameter. This is where the actual insertion of steps data occurs.

suspend fun insertSteps(healthConnectClient: HealthConnectClient) {
try {
val stepsRecord = StepsRecord(
count = 100,
startTime = START_TIME,
endTime = END_TIME,
startZoneOffset = START_ZONE_OFFSET,
endZoneOffset = END_ZONE_OFFSET,
)
healthConnectClient.insertRecords(listOf(stepsRecord))
} catch (e: Exception) {
//...
}
}

Some important tips for development

  1. You should not assume that permissions remain constant because users can grant or revoke them at any time. It is vital for your app to regularly check for granted permissions and be prepared to handle situations where permission is revoked or changed by the user. This ensures that your app can adapt to evolving user preferences and maintain proper functionality and data access.
  2. To accelerate your Health Connect development process, Google has developed a companion Android app known as the “Health Connect Developer Toolbox.” This toolbox simplifies the process of reading and writing data to Health Connect, and enables end-to-end testing without the need for a separate implementation solely for testing purposes.
  3. Do not ask users to Connect to Health Connect. You should explain the functionality that will be enabled by granting Health connect permissions.
The picture is provided by Google resources

4. Google research indicates that users will want to manage their Health Connect experience from your app’s settings. Therefore, you should make sure to include a button for easy access to Health Connect. This allows them to adjust permissions and review stored data. Besides, consider adding an option for users to connect or disconnect from Health Connect within your app.

5. Wear Health Services is an API for collecting real-time sensor data on Wear OS devices in an efficient way. While Health Connect is not available on the watch, developers can use Wear Health Services to gather data from wearables and send it to their phone app. From there, the phone app can store and share this data with Health Connect. This way, developers can make use of the rich data obtained from wearable sensors.

In conclusion

In fact, Android users want a single solution to manage their health data, pick their favorite apps, and keep things private. To tackle the challenges of working with different platforms and APIs in this area, Google has suggested Health Connect for Android. Health Connect offers an API for accessing health and wellness data stored on Android devices. It comes with a consistent data structure, and allows for precise user permission management. Also, it centralizes privacy and data management controls. This essay discussed some best-practices in Health Connect in Android development based on Google documents and resources.

--

--

Kayvan Kaseb
Software Development

Senior Android Developer, Technical Writer, Researcher, Artist, Founder of PURE SOFTWARE YAZILIM LİMİTED ŞİRKETİ https://www.linkedin.com/in/kayvan-kaseb