Permission request

Granted permission check

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()
    }
}