Health Connect Integration for Health Data Access in Android
Health Connect — centralized Android health data storage, Google's answer to HealthKit on iOS. Appeared as standalone app in 2022, built-in Android 14 as system component. Correct architecture from start saves weeks on adding new data types or Wear OS support.
Google Play Requirements and Licensing Agreement
Start here — not with code. Apps reading Health Connect data must:
- Sign Health Connect Permissions Policy and submit form to Google before publishing
- Have privacy policy screen explicitly describing health data usage
- Not transfer health data to third parties without explicit user consent (including analytics)
- Show justification for each data type when requesting permissions
Violating requirements — guaranteed Play Market removal. Complete Health Connect review before release.
Connection and Permissions
// build.gradle
implementation("androidx.health.connect:connect-client:1.1.0")
Minimum version: minSdk = 26, but Health Connect as app works on Android 9+, built-in — Android 14. On Android 9–13 user must install Health Connect from Play Store.
val healthConnectClient = HealthConnectClient.getOrCreate(context)
// Check availability
when (HealthConnectClient.getSdkStatus(context)) {
HealthConnectClient.SDK_AVAILABLE -> { /* work */ }
HealthConnectClient.SDK_UNAVAILABLE_PROVIDER_UPDATE_REQUIRED -> {
// Show install/update button
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("market://details?id=com.google.android.apps.healthdata")
}
startActivity(intent)
}
HealthConnectClient.SDK_UNAVAILABLE -> { /* Android < 9, not supported */ }
}
Request permissions:
val permissions = setOf(
HealthPermission.getReadPermission(StepsRecord::class),
HealthPermission.getReadPermission(HeartRateRecord::class),
HealthPermission.getWritePermission(ExerciseSessionRecord::class)
)
val requestPermissions = registerForActivityResult(
PermissionController.createRequestPermissionResultContract()
) { granted ->
if (granted.containsAll(permissions)) {
// all permissions granted
}
}
// Check before requesting
val granted = healthConnectClient.permissionController.getGrantedPermissions()
if (!granted.containsAll(permissions)) {
requestPermissions.launch(permissions)
}
Reading Data: Records and Queries
Each data type — separate Record class: StepsRecord, HeartRateRecord, SleepSessionRecord, ExerciseSessionRecord etc. Over 40 types.
// Steps for period
val response = healthConnectClient.readRecords(
ReadRecordsRequest(
recordType = StepsRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
)
val totalSteps = response.records.sumOf { it.count }
For aggregation — aggregateGroupByDuration or aggregateGroupByPeriod:
val aggregateRequest = AggregateGroupByPeriodRequest(
metrics = setOf(StepsRecord.COUNT_TOTAL),
timeRangeFilter = TimeRangeFilter.between(startTime, endTime),
timeRangeSlicer = Period.ofDays(1)
)
val result = healthConnectClient.aggregateGroupByPeriod(aggregateRequest)
result.forEach { bucket ->
val steps = bucket.result[StepsRecord.COUNT_TOTAL] ?: 0L
// steps per day
}
Recording Workouts
val exerciseSession = ExerciseSessionRecord(
startTime = workoutStart,
startZoneOffset = ZoneOffset.UTC,
endTime = workoutEnd,
endZoneOffset = ZoneOffset.UTC,
exerciseType = ExerciseSessionRecord.EXERCISE_TYPE_RUNNING,
title = "Morning run"
)
val distanceRecord = DistanceRecord(
startTime = workoutStart,
startZoneOffset = ZoneOffset.UTC,
endTime = workoutEnd,
endZoneOffset = ZoneOffset.UTC,
distance = Length.meters(5200.0)
)
healthConnectClient.insertRecords(listOf(exerciseSession, distanceRecord))
Background Reading and Changes
changesToken — incremental updates mechanism, like HKAnchoredObjectQuery in HealthKit:
// Get initial token
val token = healthConnectClient.getChangesToken(
ChangesTokenRequest(setOf(StepsRecord::class))
)
// On next sync
val changes = healthConnectClient.getChanges(token)
changes.changes.filterIsInstance<UpsertionChange>().forEach { change ->
// new or updated record
}
val newToken = changes.nextChangesToken // save for next time
Google Fit Compatibility
Health Connect doesn't read Google Fit history automatically. User must manually enable sync in Health Connect settings. If app migrates from Google Fit — notify users old data may display with delay.
Timeframes
Basic Health Connect integration (steps, heart rate, sleep) — 5–8 work days. With workout recording, incremental server sync and Google Fit fallback — 2–3 weeks.







