Android SDK 1.x
Connect Samsung Health and Health Connect users with the terra-android 1.x SDK: install it, initialise it, connect a user and enable background delivery.
This page shows how to integrate Android health stores in your Android app with terra-android 1.x. It covers SDK initialization, user connection, permission handling and background data updates.
The TerraAndroid SDK supports the following integrations:
Samsung Health
Health Connect
Install the SDK
Add Terra to build.gradle
Inside the dependencies section of your
build.gradlefile, add the following line of code.
implementation 'co.tryterra:terra-android:{VERSION_NUMBER}'You can find the latest version number on Maven Central.
Sync your project with build.gradle
Sync your project with Gradle by clicking the Sync Project with Gradle Files button in the toolbar. This will download the TerraAndroid SDK and make it available to your project.
Configure health permissions
Terra has a privileged partnership with Samsung that gives you direct access to the Samsung Health SDK, with no Health Connect intermediary. This is the recommended route for Samsung devices.
1. Apply for Samsung Health access
Submit your application through the Samsung Health partnership portal.
2. Install the Samsung-direct SDK
After approval, replace co.tryterra:terra-android with the separate Samsung-direct artifact:
Find the latest version on Maven Central. Note that this is a separate artifact, not a tagged version of terra-android. Your initConnection(Connections.SAMSUNG, ...) call and the rest of your code stay exactly the same.
3. Add ProGuard rule (required for release builds)
Add this to your proguard-rules.pro:
Without this, R8 minification will strip Samsung SDK classes and cause runtime crashes in production.
4. Requirements
Samsung Health app must be installed on the device
Android 28 (minSDK 28) and above
Enable Developer Mode in Samsung Health on test devices (Settings > About Samsung Health > tap version number repeatedly)
No additional manifest or Gradle changes are needed: the Terra SDK handles everything
1. In the Health Connect app
Give all permissions between the apps you wish to read from (e.g. Samsung Health, Google Fit, etc) & Health Connect.
2. Add Health Connect capability to your app
Include the permission tags under the Activity you wish to link the user to when they click the privacy policy link in the Health Connect permission screen. Here are the steps:
Go to your Android App's
AndroidManifest.xmlGo to your privacy policy
<activity>and include the following tags under this<activity>tag.
"Your Android manifest needs to have an Activity that displays your app's privacy policy, which is your app's rationale of the requested permissions, describing how the user's data is used and handled." (Health Connect)
Initialize the SDK
The initialization only needs to be done once on app start (for example in your MainActivity file or equivalent), and every time the app is brought into the foreground. This ensures that the SDK is properly set up and ready to be used.
Import Terra and TerraManager
In your Android Studio project, you should now be able to import classes from the Terra SDK.
Get a TerraManager instance
To do this, run the Terra manager initialization function as below:
This call is asynchronous. Make sure it completes before you use the manager.
Connect a user
Once Terra is initialized, you can create a connection (e.g. Samsung Health or Health Connect).
Call terra.initConnection from the TerraManager instance you've created above to have the corresponding permission screen pop up.
Call initConnection()
This method triggers a permission popup and establishes the connection.
From the TerraManager instance, call initConnection() with the following arguments:
type: Specify the connection type:Connections.SAMSUNGfor Samsung Health orConnections.HEALTH_CONNECTfor Health Connecttoken: A one-time authentication token generated from your backend server using Terra API. This ensures secure communication between your app and the Terra servers.customPermissions: A set of permissions that define what data you want to request (e.g., heart rate, steps). If empty, it defaults to all available permissions. See Permissions mapping for every value and the Health Connect (or Samsung) data type it requests.schedulerOn: Enables background delivery, so Terra keeps syncing while your app is in the background. Defaults totrue. On Health Connect connections, Terra also requests Health Connect's background-read permission as part of this flow. See Set up background delivery below.
To learn more about these parameters and their different options, check the Android SDK Reference.
Generate an auth token
To be able to call the initConnection() method, you need to pass a token as an argument.
This token is a single-use token created to ensure the authentication endpoint for creating a connection (and connecting the SDK to Terra's servers) does not get abused.
Generate the token with this endpoint POSThttps://api.tryterra.co/v2/auth/generateAuthToken . Make sure to call it with your Terra x-api-key and dev-id in the headers from your backend server. After you generate the token, provide the response to your client side using your own logic.
For every parameter, see the /auth/generateAuthToken endpoint in the API reference.
Validate the connection
To ensure a connection is still valid on the client side, use the terra.getUserId() method. This function is synchronous and returns the user_id right away or null if none exists.
Set up background delivery
Background delivery keeps Health Connect data syncing while your app is in the background, rather than only while it is open. It requires terra-android 1.7.0 or later, and we recommend 1.7.3 or later. Before that, the periodic sync could be scheduled even when the user had not granted the background-read permission, so it ran without being able to read.
Passing schedulerOn = true to initConnection (the default) is all that is needed. Terra requests Health Connect's background-read permission during the connection flow and schedules the periodic sync as soon as the user grants it.
If the user declines that permission, background sync is not scheduled. Call enableBackgroundDelivery to prompt again:
To read the current state without prompting the user, call isBackgroundDeliveryAvailable (available from terra-android 1.7.3):
It returns false if called before initConnection, which is indistinguishable from the user having declined. Call it only once a connection exists.
You now receive health data events automatically at your destination (for example, a webhook).
You can also request historical data to backfill, to verify that data exists, or as a fallback.
Check out the Android SDK reference for details about all the functions in the SDK.
Example app
Disconnect a user
In order to disconnect an SDK user, you may use the same endpoint as for Web API-based integrations, called from your backend.
Request historical data
You can request for historical data using one of the data retrieval functions.
You may set toWebhook to false if you wish for the callback function to return the data payload on the client side.
Last updated
Was this helpful?