Using Health Connect in the Cordova Wrapper
Cordova ==> Health Connect module
The Validic Inform SDK Cordova 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 Cordova. 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.
Installation
Health Connect has its own module in the Inform SDK, therefore the following modules are required to use the Health Connect integration:
- Shared/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 Cordova 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.
<platform name="android">
...
<config-file target="AndroidManifest.xml" parent="/*" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.health.READ_STEPS" />
<uses-permission android:name="android.permission.health.READ_WEIGHT" />
...
</config-file>
...
</platform>
Privacy policy
A Privacy Policy must be present on Android in order for the app to be approved for distribution. The plugin includes a simple webview, with no JS activated, to show the Privacy Policy when requested. The Privacy Policy must be formatted as an HTML page (no JS) and placed as a file with name: privacypolicy.html under the www folder of the project (in other words, the webview loads the following URL: file:///android_asset/www/privacypolicy.html
). It is possible to change that URL by changing the value inside the file platforms/android/app/src/main/res/values/CordovaPluginHealthStrings.xml
.
WARNING: This file will be reset when the plugin is removed or the Android project is regenerated.
Implementation
Please review the class documentation for more details on the functions available in ValidicHealthConnect
.
Request permissions
Before reading or subscribing to data from Health Connect the end user must grant permissions for each Health Connect Record Type.
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 hasReadAccess
method. This method takes a data type and returns a promise that resolves with a boolean indicating if the application has read access.
ValidicMobile.InformHealthConnect.hasReadAccess(
ValidicMobile.InformHealthConnect.DataType.READ_STEPS
).then(hasAccess => {
console.log(`Read access for steps: ${hasAccess}`);
}).catch(error => {
console.error(error);
});
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.
ValidicMobile.InformHealthConnect.requestPermissions([
ValidicMobile.InformHealthConnect.DataType.READ_STEPS,
ValidicMobile.InformHealthConnect.DataType.READ_SLEEP
]).then(permissions => {
console.log(permissions);
}).catch(error => {
console.error(error);
});
Managing Subscriptions
To subscribe to Health Connect data changes, use the subscribe
method. This method takes a single data type or an array of data types and returns a promise that resolves when the subscription is successful.
ValidicMobile.InformHealthConnect.subscribe([
ValidicMobile.InformHealthConnect.DataType.READ_STEPS,
ValidicMobile.InformHealthConnect.DataType.READ_SLEEP
]).then(() => {
console.log("Subscribed successfully");
}).catch(error => {
console.error(error);
});
To unsubscribe from Health Connect data changes, use the unsubscribe
method. This method takes a single data type and returns a promise that resolves when the unsubscription is successful.
ValidicMobile.InformHealthConnect.unsubscribe(
ValidicMobile.InformHealthConnect.DataType.READ_STEPS
).then(() => {
console.log("Unsubscribed successfully");
}).catch(error => {
console.error(error);
});
To retrieve the current subscriptions, use the getCurrentSubscriptions
method. This method returns a promise that resolves with an array of data types that the user is currently subscribed to.
ValidicMobile.InformHealthConnect.getCurrentSubscriptions()
.then(subscriptions => {
console.log(subscriptions);
}).catch(error => {
console.error(error);
});
Currently Supported Record Types
Supported Sample types are available in the ValidicMobile.InformHealthConnect.DataType
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 |
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 ValidicMobile.InformHealthConnect
.
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 and returns a promise that resolves with the count of records received.
// Read summaries
ValidicMobile.InformHealthConnect.readSummaries(
new Date('2023-01-01'),
new Date('2023-01-07')
)
.then(count => {
console.log(`Retrieved ${count} summaries`);
}).catch(error => {
console.error(error);
});
// Read measurements
ValidicMobile.InformHealthConnect.readMeasurements(
ValidicMobile.InformHealthConnect.DataType.READ_STEPS,
new Date('2023-01-01'),
new Date('2023-01-07')
).then(count => {
console.log(`Retrieved ${count} measurements`);
}).catch(error => {
console.error(error);
});
// Read sleep data
ValidicMobile.InformHealthConnect.readSleep(
new Date('2023-01-01'),
new Date('2023-01-07'))
.then(count => {
console.log(`Retrieved ${count} sleep records`);
}).catch(error => {
console.error(error);
});
// Read workouts
ValidicMobile.InformHealthConnect.readWorkouts(
new Date('2023-01-01'),
new Date('2023-01-07')
).then(count => {
console.log(`Retrieved ${count} workout records`);
}).catch(error => {
console.error(error);
});
// Read nutrition data
ValidicMobile.InformHealthConnect.readNutrition(
new Date('2023-01-01'),
new Date('2023-01-07')
).then(count => {
console.log(`Retrieved ${count} nutrition records`);
}).catch(error => {
console.error(error);
});
Disconnect
To disconnect from Health Connect and optionally revoke permissions, use the disconnect
method. This method takes a boolean indicating if permissions should be revoked and returns a promise that resolves when the user has been disconnected. By default the boolean is false.
ValidicMobile.InformHealthConnect.disconnect(false)
.then(() => {
console.log("Disconnected successfully");
}).catch(error => {
console.error(error);
});
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 2 days ago