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
To check permissions call checkHealthConnectPermissions
:
void checkHealthConnectPermissions() {
HCRookHealthPermissionsManager.checkHealthConnectPermissions().then((permissionsGranted) {
// Update your UI
}).catchError((exception) {
// Handle error
});
}
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((permissionsGranted) {
// 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
});
}
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((permissionsGranted) {
// 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
}
}