Usage: Availability and permissions
Check permissions and Samsung Health app availability.
Check availability
Before proceeding further, ensure that the Samsung Health app is installed and ready to be used, call
checkSamsungHealthAvailability
:
Status | Description | What to do |
---|---|---|
INSTALLED | Samsung Health is installed and ready to be used. | Proceed to check permissions |
NOT_INSTALLED | Samsung Health is not installed. | Prompt the user to install Samsung Health. |
OUTDATED | The version of Samsung Health is too old. | Prompt the user to update Samsung Health. |
DISABLED | Samsung Health is disabled. | Prompt the user to enable Samsung Health. |
NOT_READY | The user didn't perform an initial process, such as agreeing to the Terms and Conditions. | Prompt the user to open and complete the onboarding process of Samsung Health. |
rookSamsung.checkSamsungHealthAvailability().fold(
{ availability ->
// Success
},
{
// Hanlde errors
},
)
Samsung Health permissions
These are permissions used to extract data, each Samsung Health data type is subject to a different permission, below you can see the list of permissions ROOK needs:
- ACTIVITY_SUMMARY
- BLOOD_GLUCOSE
- BLOOD_OXYGEN
- BLOOD_PRESSURE
- BODY_COMPOSITION
- EXERCISE
- EXERCISE_LOCATION
- FLOORS_CLIMBED
- HEART_RATE
- NUTRITION
- SLEEP
- STEPS
- WATER_INTAKE
Check permissions
To check permissions call checkSamsungHealthPermissions
:
val hasAllSamsungHealthPermissions = rookSamsung.checkSamsungHealthPermissions(samsungPermissions).fold(
{ hasAllPermissions ->
hasAllPermissions
},
{ throwable ->
false
}
)
The previous function will check for all permissions, to check if at least one permission is granted, call
checkSamsungHealthPermissionsPartially
:
val hasSomeSamsungHealthPermissions = rookSamsung.checkSamsungHealthPermissionsPartially(samsungPermissions).fold(
{ hasSomePermissions ->
hasSomePermissions
},
{ throwable ->
false
}
)
Request permissions
To request permissions call requestSamsungHealthPermissions
:
rookSamsung.requestSamsungHealthPermissions(samsungPermissions).fold(
{
when (it) {
SHRequestPermissionsStatus.ALREADY_GRANTED -> {
// Permissions already granted, update your UI
}
SHRequestPermissionsStatus.REQUEST_SENT -> {
// Wait for broadcast result
}
}
},
{
// Handle error
}
)
This function will return a SHRequestPermissionsStatus
with 2 possible values:
- ALREADY_GRANTED: The permissions are already granted thus no request was sent.
- REQUEST_SENT: The permissions request was sent, and you can get notified if the permissions were granted or denied using a BroadcastReceiver.
The BroadcastReceiver can be registered using the SamsungHealthPermission.ACTION_SAMSUNG_HEALTH_PERMISSIONS
action and
will contain the following extras:
SamsungHealthPermission.EXTRA_SAMSUNG_HEALTH_PERMISSIONS_GRANTED
: Boolean describing if all Samsung Health permissions were granted.SamsungHealthPermission.EXTRA_SAMSUNG_HEALTH_PERMISSIONS_PARTIALLY_GRANTED
: Boolean describing if some (at least one)Samsung Health permissions were granted. Note that ifEXTRA_SAMSUNG_HEALTH_PERMISSIONS_GRANTED
is true, this will also be true.
// 1.- Create broadcast receiver
private val samsungHealthBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val allPermissionsGranted = intent?.getBooleanExtra(
/* name = */ SamsungHealthPermission.EXTRA_SAMSUNG_HEALTH_PERMISSIONS_GRANTED,
/* defaultValue = */ false
) ?: false
val permissionsPartiallyGranted = intent?.getBooleanExtra(
/* name = */ SamsungHealthPermission.EXTRA_SAMSUNG_HEALTH_PERMISSIONS_PARTIALLY_GRANTED,
/* defaultValue = */ false
) ?: false
// This will make your app more flexible by allowing it to work even if some permissions are not granted:
val permissionsGranted = allPermissionsGranted || permissionsPartiallyGranted
// Updated your UI
}
}
// 2.- Register broadcast receiver (onCreate)
ContextCompat.registerReceiver(
context,
samsungHealthBroadcastReceiver,
IntentFilter(SamsungHealthPermission.ACTION_SAMSUNG_HEALTH_PERMISSIONS),
ContextCompat.RECEIVER_EXPORTED,
)
// 3.- Request permissions
rookSamsung.requestSamsungHealthPermissions(samsungPermissions).fold(
{
when (it) {
SHRequestPermissionsStatus.ALREADY_GRANTED -> {
// Permissions already granted, update your UI
}
SHRequestPermissionsStatus.REQUEST_SENT -> {
// Wait for broadcast result
}
}
},
{
// Handle error
}
)
// 4.- Unregister broadcast receiver (onDestroy)
context.unregisterReceiver(samsungHealthBroadcastReceiver)
Customizing permissions
The requestSamsungHealthPermissions
, checkSamsungHealthPermissions
and checkSamsungHealthPermissionsPartially
functions accept a set of SamsungHealthPermission
so you can only request/check the permissions your app truly needs:
val samsungPermissions: Set<SamsungHealthPermission> = setOf(
SamsungHealthPermission.ACTIVITY_SUMMARY,
SamsungHealthPermission.BODY_COMPOSITION,
SamsungHealthPermission.EXERCISE,
SamsungHealthPermission.EXERCISE_LOCATION,
SamsungHealthPermission.HEART_RATE,
SamsungHealthPermission.SLEEP,
SamsungHealthPermission.STEPS,
)
rookSamsung.requestSamsungHealthPermissions(samsungPermissions).fold(
{ requestPermissionsStatus ->
// Success
},
{
// Handle error
},
)