Usage: Sync health data automatically
Schedule automatic syncs in background.
Background sync
Background Sync
allows to schedule an automatic syncs every hour in the background.
Each time background sync is triggered the following operations will happen:
- Sync today events:
- Activity
- Steps
- Calories
- Body Metrics
- Heart Rate
- Oxygenation
- Hydration
- Nutrition
- Blood Pressure
- Blood Glucose
- Sync today summaries
- Sleep
- If at least 4 hours have passed since the last successful sync
- If the "new" data is equal to the previous sync it won't be synced again.
- Sleep
- Sync yesterday summaries
- Sleep
- If at least 4 hours have passed since the last successful sync
- If the "new" data is equal to the previous sync it won't be synced again.
- Physical
- If at least 4 hours have passed since the last successful sync
- If the "new" data is equal to the previous sync it won't be synced again.
- Body
- If at least 4 hours have passed since the last successful sync
- If the "new" data is equal to the previous sync it won't be synced again.
- Sleep
- Sync historic data
- Activity events
- If not synced yet 29 days, starting from yesterday
- Sleep summary
- If not synced yet 29 days, starting from yesterday
- Physical summary
- If not synced yet 29 days, starting from yesterday
- Body summary
- If not synced yet 29 days, starting from yesterday
- Activity events
Permissions
To use Background Sync
you will need:
- Samsung Health permissions: Go to the main Samsung Health Permissions section to see the implementation.
Background Sync
also requires a user id, so it's possible that the
first time nothing will happen, only after a user id is configured and all necessary permissions are
granted the automatic sync will happen within the next hour.
Usage
To schedule a background sync for every hour call schedule
:
rookSamsung.schedule(enableLogs = isDebug)
We recommend adding an extra call to schedule
in the onCreate
callback of your Application class:
class MyAplication : Application() {
override fun onCreate() {
super.onCreate()
// It's a good practice to ask your users if they want to enable this behaviour
// and wrap this line inside an if which checks a preferences-stored flag
if (userAllowedBackgroundSync) {
RookSamsungObject.schedule(this, enableLogs = isDebug)
}
}
}
It's a good practice to ask your users if they want to enable this behaviour, then save their preference in local
storage and call schedule
conditionally.
To cancel any scheduled background sync call cancel
:
rookSamsung.cancel()
If you want to check the current state of Background Sync
call isScheduled
or isScheduledFlow
(Experimental):
fun howToCheckBackgroundSyncState() {
// Get latest state
coroutineScope.launch {
val isScheduled: Boolean = rookSamsung.isScheduled().getOrDefault(defaultValue = false)
// Update UI
}
// Receive real-time updates
@OptIn(ExperimentalRookSamsungApi::class)
viewModelScope.launch {
rookSamsung.isScheduledFlow().collectLatest { isScheduled: Boolean ->
// Update UI
}
}
}
Don't use isScheduled
or isScheduledFlow
to decide if you should call schedule
, if you do this future improvements
to the Background Sync behavior may not be applied, the "isScheduled" functions are only meant for UI related purposes.
Execution/Skip conditions
Each periodic execution of Background Sync
will check for some requirements, and will skip the execution if one the
following conditions is true:
- The device battery is low.
- The device storage is low.
- The device is not connected to the internet.
- The userID hasn't been configured.
- The user hasn't granted Samsung Health Permissions.
- There is an error initializing the SDK.