Hello. I'm Garan,
and I'm part of the Android Developer Relations Team.
At I/O this year, we announced Health Connect,
the platform for sharing health and fitness data between apps
on the Android ecosystem
that is both secure
and gives the user full control of their data privacy.
Today, we're going to look a little deeper
at the practicalities of keeping your app in sync
with Health Connect data.
If you're new to Health Connect, that's okay.
Head on over to developer.android.com/healthconnect
and learn about how your app can benefit from a Health Connect integration
and then come back and join us.
Let's quickly recap on the role of Health Connect.
Imagine you were developing a workout app, as shown here.
The user may use additional apps,
such as a sleep-tracking app and a nutrition app.
Each of these apps could benefit from insights from the other,
yet there is no easy way to share health and fitness data
between them.
Enter Health Connect.
Your workout app could consume data from Health Connect,
which has been supplied by the sleep app.
Perhaps if the user has had a poor night's sleep,
your workout app can make different recommendations
for exercise that day.
Equally, by feeding data to Health Connect from your app with the user's consent,
the user can realize benefits in other apps they may use.
For example, the user may also be using a nutrition app.
This app can make different recommendations
depending on the levels of activity.
But how can you keep track of what to read and write
and when?
Let's take a look first at feeding data into Health Connect from your app.
Health Connect provides a convenient API
to ensure both new and updated data can be written to it.
We'll take an example of feeding steps data
into Health Connect from your fictional workout app.
Your workout app has its own database,
and steps data is represented using the MyStepsModel class shown.
We're going to use the InsertRecords method
to perform the writing.
This simply takes a list of records and writes them to Health Connect.
However, the important step, when used for synchronization,
is to use the Metadata property.
First, specify the clientRecordID,
and use the unique key from your app's own database.
This determines whether to insert this record
as a new record
or whether to update an existing record in Health Connect,
ensuring no duplication of data.
Also, specify the clientRecordVersion,
and use a version identifier from your app's own database,
such as the last modified time.
When updating data,
Health Connect uses this to determine which record to keep.
Once you've defined your record objects,
call InsertRecords, passing in your list of records.
Health Connect will determine whether to insert
or update records accordingly.
And note, there's no need to use UpdateRecords method at all.
So, that's how to feed data to Health Connect.
Here are some practical considerations
to ensure your integration works smoothly.
For simple measurements, like a weight measurement
or a blood-pressure measurement,
write the value immediately.
For larger sets of data,
for example, all the data from a long workout,
batch the data up,
for example, a thousand records at a time,
and write at a convenient point,
for example, at the end of the workout.
Also, use WorkManager to implement a robust means
for writing the data.
Finally, be sure to handle errors.
If there is an error, adopt a retry policy,
but ensure that your app can move past this point
if the failure continues.
Now we've seen how to contribute data to Health Connect.
Let's look at how your app can consume Health Connect data.
Health Connect provides API methods for simply reading data,
such as read records and aggregate methods shown here.
However, to keep your app's data store up to date with Health Connect
on an ongoing basis,
use the Changes Sync API.
Let's look at this concept.
When you want to retrieve the changes to step-count data,
you supply a changes token and receive a list of changes
that have happened since that token was made
in addition to a new changes token to use next time.
So, how do you get hold of your first token?
I'm glad you asked.
Use getChangesToken to obtain a token.
For example, for steps as shown here.
Let's take a look now at some code for using the changes token
to get the list of available changes.
You can see here the call to get changes
supplying the token we obtained just previously.
Also notice how it is important
to distinguish between inserts, updates and deletions.
Finally, store the latest change sync token in your app,
and you're then good to go for getting the next set of changes
when you need them.
Before we move on to some practical considerations,
let's look at the changes themselves in a bit more depth.
Each change will contain an ID in the metadata.
You should store this ID in the record in your app's own database,
as this will allow you to differentiate future updates from inserts.
Just like with feeding data,
here are some best practices for ensuring a smooth experience
consuming data from Health Connect.
The changes sync API will return all the changes,
including ones that your app wrote itself.
So, be sure to filter these out using your package name.
Remember that your app can only read data from Health Connect
when it is in the foreground,
So, be sure that your implementation can handle being interrupted
and is able to pick up where it left off.
The changes sync token has a lifetime of 30 days.
You'll need to handle the case where the token has expired.
Finally, if the user can independently toggle on and off different data types,
be sure to use separate changes sync tokens for each.
Now you know how to keep your app in sync with Health Connect,
allowing your users to benefit from greater health and fitness insights
across their choice of apps.
For more information, visit the Health Connect page
at developer.google.com/healthconnect.