Using Health Connect in the Flutter Wrapper
Flutter ==> Health Connect module
The Validic Inform SDK Flutter Wrapper offers native mobile libraries which you can implement into your mobile project to power data syncing with the Android Health Connect health data aggregation platform.
The Inform SDK supports Android Health Connect for Flutter. This document lays out the installation and use of the Health Connect integration.
Android Health Connect allows health and fitness apps to store and share the same on-device data, within a unified ecosystem. It also offers a single place for users to control which apps can read and write health and fitness data.
Health Connect combines data from a variety of other fitness and health apps, including Google Fit and Samsung Health.
Attn: Google Fit Clients
If you are currently using the Google Fit SDK integration, be aware that Google is sunsetting Google Fit SDK on June 30, 2025.
You will need to migrate to using Health Connect, which is the integration that is described on this page. Please also see Migrating from Google Fit to Health Connect for more details.
Installation
Health Connect has its own module in the Inform SDK, therefore the following modules are required to use the Health Connect integration:
- Core (Session)
- Health Connect
Installation instructions are detailed in Installation.
Availability
Accessing Health Connect depends on what OS version the end user is running. Check if Health Connect is available on the device before attempting to access Health Data.
Android 14
Health Connect is a system app and is therefore available on every Android device running Android >= Android 14.
Android 13 and Below
For devices running Android Android > Android 9 and Android <= Android 13 Health Connect is available as an application on the Play Store.
Session
A valid session is required in order to use the Health Connect integration in the Validic Inform SDK. See Session for more information.
Record types
The record types used in the Validic Inform Mobile SDK are described in Record Types.
Record Identifiers
More information on record identifiers can be found in Record Identifiers.
Listening for InformRecord Upload
More information on record events can be found in Record Events.
Health Connect
This project integrates Health Connect with the Validic Mobile Inform SDK Flutter Wrapper to passively collect data on behalf of an end user. Validic ensures that our Mobile SDK makes use of the minimal scopes needed for our supported data types.
Please be aware that your app will need to be released on the Play Store in order for Google to review it prior to using the Health Connect data sync. However, you do not need to publish your app in order to test Health Connect.
Please review Google's guide to get started planning your request to Google for Health Connect data access.
Migration from Google Fit to Health Connect
Changes are in progress by Android. If your app currently supports the Validic integration to Google Fit, your users will need to be notified that they will need to take action.
Users will need to use Health Connect in order to sync Google Fit data to you once the Google Fit SDKs are sunset. For more information about Google Fit and Health Connect, please see our documentation about Migrating Users from Google Fit to Health Connect.
Availability
Accessing Health Connect depends on what OS version the end user is running. Check if Health Connect is available on the device before attempting to access Health Data.
Android 14
Health Connect is a system app and is therefore available on every Android device running Android >= Android 14.
Android 13 and Below
For devices running Android <= Android 14 Health Connect is available as an application on the Play Store.
Accessing Sensitive Data
Data stored in Health Connect is considered sensitive. Accessing end user data requires that the user grant permission to each data type before data can be read. To get started add the permission for each data type that you want to use in your application's AndroidManifest.xml
. A full list of datatypes and the necessary permissions are available in Google's documentation here.
<manifest>
<uses-permission android:name="android.permission.health.READ_STEPS"/>
<uses-permission android:name="android.permission.health.READ_WEIGHT"/>
</manifest>
Privacy policy
Health Connect requires a Privacy Policy to be defined by the client application. The privacy policy will automatically be launched when clicking the privacy policy link in the Health Connect Permissions dialog. By default a WebView is launched that directs to a www/privacypolicy.html
file in application's assets with javascript disabled. If the privacy policy is a hosted policy, then override the privacy_policy_url
string in your application's strings.xml
values file.
<string name="privacy_policy_url">https://validic.com/privacy-policy</string>
Implementation
Please review the class documentation for more details on the functions available in ValidicHealthConnect
.
Request permissions
Before subscribing to a data type, ensure that the end user has granted access to read the data type and use the requestPermissions
method to request permissions for the required data types.
Get previously granted permissions
Checking the permission status for a Health Connect Record Type will indicate if it is possible to read and subscribe to data changes for a Health Connect type.
To check if the application has read access to a specific Health Connect data type, use the isPermissionGranted
method. This method takes a data type and returns a promise that resolves with a boolean indicating if the application has read access.
bool hasPermission = await ValidicHealthConnect.isPermissionGranted(DataType.steps);
if (!hasPermission) {
Map<DataType, bool> results = await ValidicHealthConnect.requestPermissions({DataType.steps});
hasPermission = results[DataType.steps] ?? false;
}
if (!hasPermission) {
print('Permission not granted for Step Count');
}
Health Connect permissions are documented in Google's guide.
Request new permissions
Health Connect permissions are documented in Google's guide.
To request permissions for Health Connect data types, use the requestPermissions
method. This method takes an array of data types and returns a promise that resolves with a record indicating if the permission was granted.
bool hasPermission = await ValidicHealthConnect.isPermissionGranted(DataType.steps);
if (!hasPermission) {
Map<DataType, bool> results = await ValidicHealthConnect.requestPermissions({DataType.steps});
hasPermission = results[DataType.steps] ?? false;
}
if (!hasPermission) {
print('Permission not granted for Step Count');
}
Managing Subscriptions
Subscribing to datatypes requires that the end user has granted permission for a DataType. See Managing Permissions for requesting access.
You can subscribe to or unsubscribe from specific data types using the subscribe and unsubscribe methods. To view the current subscriptions, use the getCurrentSubscriptions
method.
Subscribing to Steps:
try {
await ValidicHealthConnect.subscribe(DataType.steps);
print('Subscribed to Step Count');
} catch (e) {
print('Error subscribing to Step Count: $e');
}
Unsubscribing from Steps:
try {
await ValidicHealthConnect.unsubscribe(DataType.steps);
print('unsubscribed from Step Count');
} catch (e) {
print('Error unsubscribing to Step Count: $e');
}
Currently Supported Record Types
Supported Sample types are available in the ValidicHealthConnect
module.
Refer to the following table for more details on the supported record types for Health Connect:
Health Connect Type (data type) | Inform Resource | Inform Metric |
---|---|---|
Steps | summaries | steps |
ActiveCaloriesBurned | summaries | active_energy_burned |
TotalCaloriesBurned | summaries | energy_burned |
Distance | summaries | distance |
FloorsClimbed | summaries | floors_climbed |
BasalMetabolicRate | summaries | basal_energy_burned |
HeartRate / BPM_AVG | summaries | avg_heart_rate |
HeartRate / BPM_MAX | summaries | max_heart_rate |
HeartRate / BPM_MIN | summaries | min_heart_rate |
ExerciseSession / EXERCISE_DURATION_TOTAL | summaries | active_duration |
Weight | measurements | body_weight |
BloodGlucose | measurements | blood_glucose |
BloodPressure / diastolic | measurements | diastolic |
BloodPressure / systolic | measurements | systolic |
OxygenSaturation | measurements | spo2 |
BodyTemperature | measurements | body_temperature |
BodyFat | measurements | body_fat |
BodyWaterMass | measurements | body_water_mass |
BoneMass | measurements | body_bone_mass |
LeanBodyMass | measurements | body_lean_mass |
Height | measurements | body_height |
ExerciseSession / EXERCISE_DURATION_TOTAL | workouts | workout_duration |
SleepSession / SLEEP_DURATION_TOTAL | sleep | sleep_duration |
SleepSession / STAGE_TYPE_LIGHT | sleep | light_sleep |
SleepSession / STAGE_TYPE_DEEP | sleep | deep_sleep |
SleepSession / STAGE_TYPE_REM | sleep | rem_sleep |
Nutrition (calcium) | nutrition | calcium |
Nutrition / totalCarbohydrate | nutrition | carbohydrate |
Nutrition / cholesterol | nutrition | cholesterol |
Nutrition / dietaryFiber | nutrition | dietary_fiber |
Nutrition / energy | nutrition | energy_consumed |
Nutrition / totalFat | nutrition | fat |
Nutrition / protein | nutrition | protein |
Nutrition / saturatedFat | nutrition | saturated_fat |
Nutrition / sodium | nutrition | sodium |
Nutrition / sugar | nutrition | sugars |
Nutrition / unsaturatedFat | nutrition | unsaturated_fat |
================================================ Needs more details below
InformRecords collected are automatically submitted to the Validic Inform Mobile API and a summary of records processed will be sent to any listener registered using ValidicHealthConnect
.
Fetch historical data
The Health Connect integration supports retrieving 30 days of historical step data.
To read historical data from Health Connect, use the appropriate read method for the data type. Each method takes a start date and an end date.
try {
final startTime = DateTime.now().subtract(const Duration(days: 6));
final endTime = DateTime.now();
final summaryCount = await ValidicHealthConnect.readHistory(
InformRecordType.summary,
startDate: startTime,
endDate: endTime,
);
print('Read $summaryCount step summaries for the last 7 days');
} catch (e) {
print('Error reading step summaries: $e');
}
Disconnect
To disconnect from Health Connect and optionally revoke permissions, use the native Android method.
Listening to Health Connect API Exceptions
Errors thrown by the Health Connect Platform are Logged and then re-thrown. More information on those errors can be found in Android's documentation here.
Updated 1 day ago