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
- Temperature
- 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
Please be sure to enable Continuous HR measurement on your Galaxy Watch to improve the accuracy of the readings.
Permissions
To use Background Sync you will need:
- Samsung Health permissions: Go to the main Samsung Health Permissions section to see the implementation.
- Alarms permissions (Optional): Go to the main Alarm permissions section to see the implementation.
Background Sync uses the SCHEDULE_EXACT_ALARM permission to improve its lifetime in battery constrained scenarios,
however this is optional, you can skip requesting this permission to your users and still use Background Sync
normally. Background Sync will only schedule an alarm if the permission is granted.
IMPORTANT: You need to call enableBackground again after requesting the alarms' permission.
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 enableBackground:
void enableBackgroundSync() async {
try {
await RookSamsung.enableBackground(enableNativeLogs: isDebug);
// Background sync enabled
} catch (error) {
// Handle error
}
}
We recommend adding an extra call to enableBackground in the main method:
void main() {
// Ensure that the plugin is ready
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isAndroid) {
enableAndroidBackgroundSync();
} else {
enableIOSBackgroundSync();
}
runApp(App());
}
void enableAndroidBackgroundSync() async {
try {
final userAllowedBackgroundSync = await AppPreferences().getUserAllowedBackgroundSync();
if (userAllowedBackgroundSync) {
await RookSamsung.enableBackground(enableNativeLogs: isDebug);
}
} catch (error) {
// Log
}
}
void enableIOSBackgroundSync() async {
// Go to IOS documentation to learn how to enable background sync
}
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 enableBackground conditionally.
To cancel any scheduled background sync call disableBackground:
void disableBackgroundSync() async {
try {
await RookSamsung.disableBackground();
// Background sync disabled
} catch (error) {
// Handle error
}
}
If you want to check the current state of Background Sync call isScheduled or isScheduledUpdates (Experimental):
void checkBackgroundSyncState() async {
try {
final isScheduled = await RookSamsung.isScheduled();
// Update your UI
} catch (error) {
// Handle error
}
}
// 1.- Create a stream subscription
StreamSubscription<bool>? streamSubscription;
// 2.- Listen to stream
streamSubscription = RookSamsung.isScheduledUpdates.listen((isScheduled) {
// Update UI
});
// 3.- Stop listening to the stream
streamSubscription?.cancel();
Don't use isScheduled or isScheduledUpdates to decide if you should call enableBackground, 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 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.