Accessing health data requires the user’s permissions for every data type. Check that the permissions are granted using:
HealthDataStore.getGrantedPermissions()
Calling this method can result in throwing HealthDataException. There are several types of exception classes inheriting from the mentioned parent class: AuthorizationException, InvalidRequestException, PlatformInternalExcpetion and ResolvablePlatformException. The last one of them indicates that Samsung Health is not ready to serve the specified operation. This kind of error can be resolved by the user at runtime in some cases by calling resolve() method.
ResolvablePlatformException can have one of the following error codes, which might help in applying appropriate action to the user:
ErrorCode.ERR_PLATFORM_NOT_INSTALLED: Samsung Health is not installed.
ErrorCode.ERR_OLD_VERSION_PLATFORM: The version of Samsung Health is old so it can't support this function of the SDK.
ErrorCode.ERR_PLATFORM_DISABLED: Samsung Health has been installed but it is disabled.
ErrorCode.ERR_PLATFORM_NOT_INITIALIZED: Samsung Health has been installed but the user didn't perform an initial process, such as agreeing to the Terms and Conditions.
For more details, please refer to com.samsung.android.sdk.health.data.error package of Samsung Health Data SDK’s API Reference.
Request for permissions
If all permissions were not present, we need to request for permissions with:
HealthDataStore.requestPermissions()
If this API call succeeds, a data permission popup will display on the app activity with a list of permissions to grant by the user. After the user allows permissions through the data permission popup, the API returns a set of granted permissions.
Below there’s an example of permission popup that is shown to the user:
Below is an example code for permission checking, which covers all the topics discussed in this section:
suspend fun checkForPermissions(activity: Activity) {
val permSet = mutableSetOf(
Permission.of(DataTypes.HEART_RATE, AccessType.READ),
Permission.of(DataTypes.STEPS, AccessType.READ),
Permission.of(DataTypes.SLEEP_GOAL, AccessType.READ),
Permission.of(DataTypes.SLEEP, AccessType.READ),
Permission.of(DataTypes.BLOOD_OXYGEN, AccessType.READ),
Permission.of(DataTypes.BLOOD_GLUCOSE, AccessType.READ),
Permission.of(DataTypes.BLOOD_GLUCOSE, AccessType.WRITE))
try {
val healthDataStore = HealthDataService.getStore(activity.applicationContext)
val grantedPermissions = healthDataStore.getGrantedPermissions(permSet)
if (grantedPermissions.containsAll(permSet)) {
// All Permissions already granted
} else {
// Partial or No permission, we need to request for the permission popup
healthDataStore.requestPermissions(permSet, activity)
}
} catch (error: HealthDataException) {
if (error is ResolvablePlatformException && error.hasResolution) {
error.resolve(activity)
}
// handle other types of HealthDataException
error.printStackTrace()
}
}
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.