Check, install and update Google Health Connect, and request historical and background data access on Android.
Copy MarkdownOpen inOpen in GitHubView as MarkdownOpen in Scira AIOpen in ChatGPTOpen in ClaudeOpen in Cursor
Google Health Connect is Android's centralized health data repository. Unlike HealthKit, which is built into iOS, Health Connect may not be preinstalled on every Android device. The plugin provides APIs to check its availability, request its installation, and manage Health Connect permissions.
Method
Returns
Purpose
getHealthConnectSdkStatus()
Future<HealthConnectSdkStatus?>
Get the Health Connect SDK status.
isHealthConnectAvailable()
Future<bool>
Check whether Health Connect is installed and ready.
installHealthConnect()
Future<void>
Open the app store to install or update Health Connect.
isHealthDataHistoryAvailable()
Future<bool>
Check whether the device supports historical data access.
isHealthDataHistoryAuthorized()
Future<bool>
Check whether historical data access is granted.
requestHealthDataHistoryAuthorization()
Future<bool>
Request access to historical data.
isHealthDataInBackgroundAvailable()
Future<bool>
Check whether background data access is supported.
It returns a HealthConnectSdkStatus value, or null on error:
Status
Value
Description
sdkAvailable
3
Health Connect is installed and ready.
sdkUnavailableProviderUpdateRequired
2
The Health Connect provider needs an update.
sdkUnavailable
1
Health Connect is not available.
final status = await health.getHealthConnectSdkStatus();switch (status) { case HealthConnectSdkStatus.sdkAvailable: print('Health Connect is ready'); break; case HealthConnectSdkStatus.sdkUnavailableProviderUpdateRequired: print('Health Connect needs update'); break; case HealthConnectSdkStatus.sdkUnavailable: print('Health Connect not installed'); break; case null: print('Error checking status');}
isHealthConnectAvailable() is a boolean check for Health Connect.
Future<bool> isHealthConnectAvailable()
It returns true when Health Connect is installed and ready, and false when it is unavailable or needs an update.
bool available = await health.isHealthConnectAvailable();if (!available) { // Prompt user to install _showHealthConnectInstallDialog();}
Always check availability before any health operation:
Future<void> ensureHealthConnectReady() async { if (!await health.isHealthConnectAvailable()) { throw HealthException( null, 'Health Connect is not available. Please install it first.', ); }}Future<void> fetchHealthData() async { await ensureHealthConnectReady(); // Now safe to proceed final data = await health.getHealthDataFromTypes(...);}
Health Connect allows reading historical data, older than 30 days, if the user explicitly authorizes it. The manifest baseline in Android setup declares the optional READ_HEALTH_DATA_HISTORY permission for historic reading. All three methods return Future<bool>:
Method
Returns true
Returns false
isHealthDataHistoryAvailable()
The device supports the historical data permission.
The feature is not available (older devices or SDK).
isHealthDataHistoryAuthorized()
Historical data access is granted.
Access is not granted, or the feature is unavailable.
requestHealthDataHistoryAuthorization()
Permission is granted.
Permission is denied, or an error occurred.
Check whether the device supports historical data access:
bool historyAvailable = await health.isHealthDataHistoryAvailable();if (historyAvailable) { print('Can request historical data access');} else { print('Historical data not supported on this device');}
With historical access granted, you can fetch data from any time period:
Future<void> fetchLongTermData() async { // Ensure historical access final hasHistoricalAccess = await health.isHealthDataHistoryAuthorized(); if (!hasHistoricalAccess) { final granted = await health.requestHealthDataHistoryAuthorization(); if (!granted) { throw Exception('Historical data access required for this feature'); } } // Now can fetch data from any time period final data = await health.getHealthDataFromTypes( types: [HealthDataType.WEIGHT], startTime: DateTime.now().subtract(Duration(days: 365)), // 1 year ago endTime: DateTime.now(), ); print('Fetched ${data.length} historical data points');}
Health Connect supports reading health data in the background. The manifest baseline in Android setup declares the optional READ_HEALTH_DATA_IN_BACKGROUND permission for background reading. All three methods return Future<bool>:
Method
Returns true
Returns false
isHealthDataInBackgroundAvailable()
The background access feature is available.
The feature is not supported.
isHealthDataInBackgroundAuthorized()
Background access is granted.
Access is not granted, or the feature is unavailable.
Register a periodic background task with WorkManager or a similar scheduler once background access is granted:
Future<void> initializeHealthSync() async { // Request background permission final hasBackgroundAccess = await health.isHealthDataInBackgroundAuthorized(); if (!hasBackgroundAccess) { final granted = await health.requestHealthDataInBackgroundAuthorization(); if (!granted) { print('Background sync not available - using foreground only'); return; } } // Setup WorkManager or similar for background tasks Workmanager().registerPeriodicTask( 'health-sync', 'syncHealthData', frequency: Duration(hours: 1), );}