Our Hubro Connect library for Android integrates with Health Connect. This integration facilitates a collection of data from Health Connect and transfer of this data to the backend storage.
In your AndroidManifest.xml make sure you include all necessary permissions with respect to types of data types that you will be accessing. An example below demonstrates the correct settings of permissions when the access is needed to steps and heart rate data.
For the full reference check for the list of all health permissions at the list of classes.
Additionally, you will need to create, or reuse existing Activity to present privacy policy and register this Activity in your AndroidManifest.xml with the following Intents.
<application>
...
<!-- For supported versions through Android 13, create an activity to show the rationale
of Health Connect permissions once users click the privacy policy link. -->
<activity
android:name=".PermissionsRationaleActivity"
android:exported="true">
<intent-filter>
<action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE"/>
</intent-filter>
</activity>
<!-- For versions starting Android 14, create an activity alias to show the rationale
of Health Connect permissions once users click the privacy policy link. -->
<activity-alias
android:name="ViewPermissionUsageActivity"
android:exported="true"
android:targetActivity=".PermissionsRationaleActivity"
android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
<intent-filter>
<action android:name="android.intent.action.VIEW_PERMISSION_USAGE"/>
<category android:name="android.intent.category.HEALTH_PERMISSIONS"/>
</intent-filter>
</activity-alias>
...
</application>
Privileges of our app as viewed in Health Connect. Button 'Read privacy policy' will open the activity stated in AndroidManifest.xml
Once the initial setup is finished, you can implement the authorization process to Health Connect. During this step you need to provide all record types you would like to access once the authorization is finished.
import no.hubroplatform.hubroconnect.ManagerFactory
val healthConnectManager = ManagerFactory().createHealthManager()
val readTypes = listOf(HealthDataType.Steps)
healthConnectManager.requestAuthorization(readTypes = readTypes, writeTypes = emptyList())
If the user authorizes the app to access listed record types, you can start interacting with the Health Connect. The following example fetches steps data collected within the last 24 hours.
You can use the Hubro Connect library to continuously fetch data from Health Connect store and transfer it to the Hubro backend. To facilitate this, we have instrumented a data sync manager, that takes care of this task continuously, even if the application is running in the background. The frequency of data retrieval and its transfer to the Hubro backend can be configured, however, it cannot be more frequent than every 15 minutes.
Before instantiating the DataSyncManager class, you need to have a valid Hubro session (user needs to be logged in). In addition, user must authorize all enlisted read data types before the collection can start (see the code snippet above).
// val session - a valid Hubro session
val readTypes = listOf(HealthDataType.Steps)
dataSyncManager = ManagerFactory().createDataSyncManager(
session,
readTypes
)
// Start data collection in 15 minutes intervals
dataSyncManager.startCollection(15)