Android (Kotlin)
Sync your users' Health Connect and Samsung Health data to Sonar from your Android app.
Early access
The Android SDK is in early access: we roll it out with a first group of customers before it opens to everyone. Contact us to join. Details on this page can still change during early access.
The Android SDK reads Health Connect and sends the data to Sonar for the signed-in user. It works like the iOS SDK: your backend mints client tokens, the app connects the provider, and the SDK syncs on its own. It sends recent days first, then works back through the history period, two years by default, then new data. Supported Native Data shows what it reads.
A direct Samsung Health integration is part of the same early access.
Requirements
| Area | Requirement |
|---|---|
| Android | 8.0 (API level 26) or later |
| Health Connect | Built into Android 14 and later. On Android 13 and earlier, the user installs the Health Connect app from Google Play |
| Google Play | Apps that read Health Connect data must complete Google’s health-data declaration in the Play Console |
| Backend | A client-token route and a Sonar user per user, as for iOS. See SDK Authentication |
| Atlas | An Android app registration with your package name, which gives the app ID |
Install
Add Sonar’s repository in settings.gradle.kts, and the SDK to your app module:
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://raw.githubusercontent.com/Sonar-Health/sonar-android-sdk/main/maven")
}
}dependencies {
implementation("co.sonarhealth:sonar-sdk:<version>")
}Set Up the App
Show Your Privacy Policy to Health Connect
Health Connect shows the permission sheet only when your app has an activity that shows your privacy policy. Add one that handles ACTION_SHOW_PERMISSIONS_RATIONALE, and on Android 14 and later an alias for VIEW_PERMISSION_USAGE:
<activity
android:name=".PrivacyPolicyActivity"
android:exported="true">
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
</intent-filter>
</activity>
<activity-alias
android:name="ViewPermissionUsageActivity"
android:exported="true"
android:permission="android.permission.START_VIEW_PERMISSION_USAGE"
android:targetActivity=".PrivacyPolicyActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
<category android:name="android.intent.category.HEALTH_PERMISSIONS" />
</intent-filter>
</activity-alias>Let the SDK Add the Rest
The SDK’s manifest merges into your app. It adds the Health Connect read permissions, INTERNET, and the activity that shows the permission sheet. You do not declare them yourself.
Configure at Launch
Call Sonar.configure(context, appId) in Application.onCreate, on every launch:
import android.app.Application
import co.sonarhealth.sdk.Sonar
class App : Application() {
override fun onCreate() {
super.onCreate()
Sonar.configure(this, "app_Qm3t8Vx1LmN0aB7cD9eF2g")
}
}Sonar.configure(context, appId, historyDays) sets how many days of history a new connection imports, from 30 to 1825. The default is 730.
Authenticate and Connect
Call authenticate as soon as your app knows who is signed in: right after sign-in, and at every launch once your app has restored a signed-in user. The token provider asks your backend for a client token. userId is your own stable ID for the signed-in user. If it changes, the SDK signs the previous user out first, so one user’s data never syncs into another’s account.
import co.sonarhealth.sdk.NativeProvider
import co.sonarhealth.sdk.Sonar
lifecycleScope.launch {
Sonar.authenticate(currentUser.id) { context ->
customerApi.createSonarClientToken(
appId = context.appId,
installationId = context.installationId,
).clientToken
}
val observation = Sonar.observeState { state -> render(state) }
Sonar.connect(NativeProvider.HEALTH_CONNECT)
}authenticate,connect,disconnect,sync,resync,signOut, andsendDiagnosticsare suspending functions.configure,getState,getProviderState, andobserveStateare not.observeStatecalls you on the main thread with the current state, then on every change. It returns anAutoCloseable: callclose()when you no longer need updates.observeWorkoutscalls you on the main thread after Sonar accepts workouts new on this phone, never for the history import. It returns anAutoCloseable. See New Workouts.connectshows the Health Connect permission sheet, records the connection in Sonar, and starts syncing.- Failed calls throw
SonarSdkError, with acode, aretryableflag, and arecoveryAction.
The calls, results, states, and errors are the ones in the SDK Interface. Kotlin enum values are upper case (SessionStatus.READY, NativeProvider.HEALTH_CONNECT). diagnostics and setVerboseDiagnostics also take a Context.
Permissions
- The sheet asks for every Health Connect type in Supported Native Data, plus access to past data and background reads where the device supports them.
- Unlike iOS, Health Connect reports what the user granted. The permission status is
granted,partial, ordenied. If the user grants nothing,connectthrowspermission_denied. - If the user refuses access to past data, Health Connect shares only recent data. If they refuse background reads, the SDK syncs only while your app is open.
- If Health Connect is missing or out of date,
connectthrowsprovider_install_requiredorprovider_update_required. Send the user to Google Play.
React Native Apps on Android
@sonarhealth/react-native-sdk builds and runs on Android today, so one codebase ships to both platforms. The package turns on Health Connect when the Android SDK opens to everyone. Until then, Sonar calls on Android reject with platform_unsupported, so call Sonar only when Platform.OS === "ios". See React Native.
Cloud Providers on Android
Wearables with a cloud API, such as Garmin, Oura or WHOOP, connect to Sonar from any phone. Your backend connects them through the API: see Connect a Device.
Sonar