Usage: Availability and permissions
Get availability status, check and ask for permissions.
Check availability
Before proceeding further, you need to ensure the user's device is compatible with Health Connect and check if the APK is installed.
Call checkHealthConnectAvailability
and take the corresponding actions:
Status | Description | What to do |
---|---|---|
installed | APK is installed | Proceed to check permissions |
notInstalled | APK is not installed | Prompt the user to install Health Connect. |
notSupported | This device does not support Health Connect | Take the user out of the Health Connect section |
void checkAvailability() {
HCRookHealthPermissionsManager.checkHealthConnectAvailability().then((availability) {
// Success
}).catchError((exception) {
// Handle error
});
}
Health Connect permissions
These are permissions used to extract data, each Health Connect data type is subject to a different permission, below you can see the list of permissions ROOK needs:
- READ_SLEEP
- READ_STEPS
- READ_DISTANCE
- READ_FLOORS_CLIMBED
- READ_ELEVATION_GAINED
- READ_OXYGEN_SATURATION
- READ_VO2_MAX
- READ_TOTAL_CALORIES_BURNED
- READ_ACTIVE_CALORIES_BURNED
- READ_HEART_RATE
- READ_RESTING_HEART_RATE
- READ_HEART_RATE_VARIABILITY
- READ_EXERCISE
- READ_SPEED
- READ_WEIGHT
- READ_HEIGHT
- READ_BLOOD_GLUCOSE
- READ_BLOOD_PRESSURE
- READ_HYDRATION
- READ_BODY_TEMPERATURE
- READ_RESPIRATORY_RATE
- READ_NUTRITION
- READ_MENSTRUATION
- READ_POWER
Check permissions
To check permissions call checkHealthConnectPermissions
:
void checkHealthConnectPermissions() {
HCRookHealthPermissionsManager.checkHealthConnectPermissions().then((permissionsGranted) {
// Update your UI
}).catchError((exception) {
// Handle error
});
}
The previous function will check for all permissions, to check if at least one permission is granted, call
checkHealthConnectPermissionsPartially
:
void checkHealthConnectPermissionsPartially() {
HCRookHealthPermissionsManager.checkHealthConnectPermissionsPartially().then((permissionsPartiallyGranted) {
// Update your UI
}).catchError((error) {
// Handle error
});
}
Request permissions
To request permissions call requestHealthConnectPermissions
:
void requestHealthConnectPermissions() {
HCRookHealthPermissionsManager.requestHealthConnectPermissions().then((requestPermissionsStatus) {
if (requestPermissionsStatus == RequestPermissionsStatus.alreadyGranted) {
// Permissions already granted, update your UI
} else {
// Wait for result in stream
}
}).catchError((error) {
// Handle error
});
}
This function will return a RequestPermissionsStatus
with 2 possible values:
- alreadyGranted: The permissions are already granted thus no request was sent.
- requestSent: The permissions request was sent, and you can get notified if the permissions were granted or denied
using the
requestHealthConnectPermissionsUpdates
stream.
// 1.- Create a stream subscription
StreamSubscription<bool>? streamSubscription;
// 2.- Listen to stream
streamSubscription = HCRookHealthPermissionsManager.requestHealthConnectPermissionsUpdates.listen((permissionsSummary) {
// Updated your UI
});
// 3.- Request permissions
HCRookHealthPermissionsManager.requestHealthConnectPermissions().then((requestPermissionsStatus) {
if (requestPermissionsStatus == RequestPermissionsStatus.alreadyGranted) {
// Permissions already granted, update your UI
} else {
// Wait for result in stream
}
}).catchError((error) {
// Handle error
});
// 4.- Stop listening to the stream
streamSubscription?.cancel();
Health Connect permissions denied
If the user clicks cancel or navigates away from the permissions screen, Health Connect will consider it as a denial of permissions. If the user denies the permissions twice, your app will be blocked by Health Connect and your only option will be to open the Health Connect app and ask your users to grant permissions manually.
When your app is blocked, any permissions request will be ignored.
To solve this problem, we recommend including an Open Health Connect
button in your permissions UI. This button will
use HCRookHealthPermissionsManager.openHealthConnectSettings()
to open the Health Connect application.
void openHealthConnect() {
HCRookHealthPermissionsManager.openHealthConnectSettings().then((_) {
// Success
}).catchError((exception) {
// Handle error
});
}
Customizing permissions
If you want to reduce the Health Connect permissions used by this SDK you can do it following the manifest merge documentation and the SDK will change the behavior of request/check permissions functions based on the declared permissions:
For example if you remove the READ_MENSTRUATION
permission...
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission
android:name="android.permission.health.READ_MENSTRUATION"
tools:node="remove"/>
</manifest>
the functions checkHealthConnectPermissions
and requestHealthConnectPermissions
will check/request all permissions
excluding READ_MENSTRUATION
Note that this process only works for removing Health Connect permissions, adding a permission that is not included in the default list will do nothing.
Health Connect permissions are declared with the syntax: android.permission.health.READ.., to see the current Health Connect permissions go to you AndroidManifest and select the merged manifest tab.
Revoke permissions
You can reset all granted Health Connect permissions back to their original state with revokeHealthConnectPermissions
:
void revokeHealthConnectPermissions() {
HCRookHealthPermissionsManager.revokeHealthConnectPermissions().then((_) {
// Health Connect permissions revoked, restart the app to apply the changes
}).catchError((error) {
// Error revoking Health Connect permissions
});
}
You may notice that after calling this function the permissions are still granted, however they will be revoked the next time the app is closed (process stops).
Android permissions
Android permissions are the normal permission every non-health app may need, in this case we use the following permissions to track steps and/or automatically sync health data:
- POST_NOTIFICATIONS
- ACTIVITY_RECOGNITION
- FOREGROUND_SERVICE
- FOREGROUND_SERVICE_HEALTH
To check permissions call checkAndroidPermissions
:
void checkAndroidPermissions() {
HCRookHealthPermissionsManager.checkAndroidPermissions().then((permissionsGranted) {
// Update your UI
}).catchError((exception) {
// Handle error
});
}
To request permissions call requestAndroidPermissions
:
void requestAndroidPermissions() async {
try {
final requestPermissionsStatus = await HCRookHealthPermissionsManager.requestAndroidPermissions();
if (requestPermissionsStatus == RequestPermissionsStatus.alreadyGranted) {
// Permissions already granted, update your UI
} else {
// Wait for result in stream
}
} catch (error) {
// Handle error
}
}
You can use HCRookHealthPermissionsManager.shouldRequestAndroidPermissions(activity)
to know if the
permissions dialog WILL BE displayed before calling requestAndroidPermissions
and
manage this scenario with more anticipation.
This function will return a RequestPermissionsStatus
with 2 possible values:
- alreadyGranted: The permissions are already granted thus no request was sent.
- requestSent: The permissions request was sent, and you can get notified if the permissions were granted or denied
using the
requestAndroidPermissionsUpdates
stream.
// 1.- Create a stream subscription
StreamSubscription<bool>? streamSubscription;
// 2.- Listen to stream
streamSubscription = HCRookHealthPermissionsManager.requestAndroidPermissionsUpdates.listen((permissionsSummary) {
// Updated your UI
});
// 3.- Request permissions
try {
final requestPermissionsStatus = await HCRookHealthPermissionsManager.requestAndroidPermissions();
if (requestPermissionsStatus == RequestPermissionsStatus.alreadyGranted) {
// Permissions already granted, update your UI
} else {
// Wait for result in stream
}
} catch (error) {
// Handle error
}
// 4.- Stop listening to the stream
streamSubscription?.cancel();
Android permissions denied
If a user denies a permission Android will not show a dialog the next time you ask for permissions,
use HCRookHealthPermissionsManager.shouldRequestAndroidPermissions()
to check if the user has
previously denied the permissions and based on the result request permissions or navigate the user to your app's
settings.
void requestAndroidPermissions() async {
try {
final shouldRequestPermissions = await HCRookHealthPermissionsManager.shouldRequestAndroidPermissions();
if (shouldRequestPermissions) {
// Request permissions
} else {
// Open your application's settings
// Show a toast indicating your users that they must enable permissions manually
}
} catch (error) {
// Handle error
}
}