Usage: Sync health data manually
Sync Health Connect data by yourself.
The automatic implementation is faster and easier to integrate and is what most developers need, try to use it before attempting to create a custom sync process.
Sync health data manually
There are 2 types of health data Summaries and Events.
Health Data | Timezone | Oldest date of retrieval | Latest date of retrieval | Class |
---|---|---|---|---|
Summary | UTC | 29 days | Yesterday | HCRookSyncManager |
Event | UTC | 29 days | Today | HCRookSyncManager |
Sync summaries
Use sync(bool enableLogs)
to sync the last 29 days of SLEEP_SUMMARY, PHYSICAL_SUMMARY and BODY_SUMMARY (not
including today).
If the app goes to the background the synchronization will fail, use scheduleYesterdaySync. instead if you want the synchronization to continue until it finishes (scheduleYesterdaySync will not only sync summaries, but also events).
void syncSummariesHistoric() async {
try {
await HCRookSyncManager.sync(enableLogs: isDebug);
// Historic summaries sync started
} catch (error) {
// Handle error
}
}
Use sync(DateTime date)
to sync SLEEP_SUMMARY, PHYSICAL_SUMMARY and BODY_SUMMARY for the provided date
.
void syncSummaries() async {
try {
await HCRookSyncManager.sync(date: date);
// Summaries synced successfully
} catch (error) {
// Handle error
}
}
Use sync(DateTime date, HCSummarySyncType summary)
to sync the summary
of choice for the provided date
.
void syncSingleSummary() async {
try {
await HCRookSyncManager.sync(date: date, summary: summarySyncType);
// Summary synced successfully
} catch (error) {
// Handle error
}
}
Sync events
Use syncEvents(DateTime date, HCEventSyncType event)
to sync the event
of choice for the provided date
.
void syncSingleEvent() async {
try {
await HCRookSyncManager.syncEvents(date, eventSyncType);
// Event synced successfully
} catch (error) {
// Handle error
}
}
Sync current day events
Current day events allow to send small amounts of health data, but they also return the data that was sent, you can use this data to update your UI without waiting for the webhook event.
Steps events
Retrieve and upload current day steps count of Health Connect.
void syncStepsEvents() async {
try {
final syncStatusWithData = await HCRookSyncManager.getTodayStepsCount();
switch (syncStatusWithData) {
case Synced(data: final steps):
// Update the UI with steps count
break;
case RecordsNotFound():
// No steps events found
break;
}
} catch (error) {
// Handle error
}
}
Warning: This function contributes to the Health Connect rate limit, don't call it too frequently.
Calories events
Retrieve and upload current day calories count of Health Connect.
void syncCaloriesEvents() async {
try {
final syncStatusWithData = await HCRookSyncManager.getTodayCaloriesCount();
switch (syncStatusWithData) {
case Synced(data: final calories):
// Update the UI with calories count
break;
case RecordsNotFound():
// No calories events found
break;
}
} catch (error) {
// Handle error
}
}
Warning: This function contributes to the Health Connect rate limit, don't call it too frequently.