Skip to main content

Usage: Sync health data manually

Sync Health Connect data by yourself.

info

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 DataTimezoneOldest date of retrievalLatest date of retrievalClass
SummaryUTC29 daysYesterdayRookSummaryManager
EventUTC29 daysTodayRookEventManager

Create the required instances:

val rookSummaryManager = RookSummaryManager(rookConfigurationManager)
val rookEventManager = RookEventManager(rookConfigurationManager)
note

The rookConfigurationManager instance should be same that you created and initialized in the previous steps.

Sync summaries

To sync any type of summary, you need to provide a date. This date cannot be the current day and cannot be older than 29 days. See the examples below:

Current dateProvided dateIs valid?
2023-01-082023-01-08No, the date is from today
2023-01-082023-01-07Yes, the date is from yesterday
2023-01-082022-11-01No, the date is older than 29 days
2023-01-082023-01-01Yes, the date is 7 days old

To get health data, call sync_xxx_summary and provide a LocalDate instance of the day you want to retrieve the data from.

For example, if you want to sync yesterday's sleep summary, call syncSleepSummary.

val yesterday = LocalDate.now().minusDays(1)
val result = rookSummaryManager.syncSleepSummary(yesterday)

result.fold(
{
// Sleep summary synced successfully
},
{
// Error syncing Sleep summary

val error = when (it) {
is SDKNotInitializedException -> "SDKNotInitializedException: ${it.message}"
is UserNotInitializedException -> "UserNotInitializedException: ${it.message}"
is HealthConnectNotInstalledException -> "HealthConnectNotInstalledException: ${it.message}"
is DeviceNotSupportedException -> "DeviceNotSupportedException: ${it.message}"
is MissingPermissionsException -> "MissingPermissionsException: ${it.message}"
is TimeoutException -> "TimeoutException: ${it.message}"
is HttpRequestException -> "HttpRequestException: code: ${it.code} message: ${it.message}"
else -> "${it.message}"
}
}
)

Sync pending summaries

When you call sync_xxx_summary, what happens it's that:

  1. Health data is extracted from Health Connect
  2. The extracted data is enqueued
  3. The enqueued data is uploaded to ROOK servers.
  4. If success the queued is cleared. Otherwise, the summary is stored.

To retry sending those stored summaries call syncPendingSummaries

val result = rookSummaryManager.syncPendingSummaries()

result.fold(
{
// Pending summaries synced successfully
},
{
// Error syncing pending summaries

val error = when (it) {
is SDKNotInitializedException -> "SDKNotInitializedException: ${it.message}"
is UserNotInitializedException -> "UserNotInitializedException: ${it.message}"
is TimeoutException -> "TimeoutException: ${it.message}"
is HttpRequestException -> "HttpRequestException: code: ${it.code} message: ${it.message}"
else -> "${it.message}"
}
}
)

Recommendations

Summaries are collections of health data from a past day, so you should not sync them more than once a day.

/**
* This function assumes that you have already checked availability and permissions.
*/
suspend fun syncSummaries() {
val yesterday = LocalDate.now().minusDays(1)

rookSummaryManager.syncSleepSummary(yesterday).fold(
{
// Sleep summary synced successfully
},
{
// Error syncing Sleep summary
}
)

// Sync other types of summaries...

// Finally you can call syncPendingSummaries to retry all failed uploads
// rookSummaryManager.syncPendingSummaries() (optional)
}
  • Be cautions of syncing summaries an excessive amount of times, Health Connect has a daily usage limit and your app could be blocked for some hours or a full day.

Sync events

To sync any type of event, you need to provide a date. This date cannot be older than 29 days. See the examples below:

Current dateProvided dateIs valid?
2023-01-082023-01-08Yes, the date is from today
2023-01-082023-01-07Yes, the date is from yesterday
2023-01-082022-11-01No, the date is older than 29 days
2023-01-082023-01-01Yes, the date is 7 days old

To get health data, call sync_xxx_events and provide a LocalDate instance of the day you want to retrieve the data from.

For example, if you want to sync today's physical events, call syncPhysicalEvents.

val result = rookEventManager.syncPhysicalEvents(today)

result.fold(
{
// Physical events synced successfully
},
{
// Error syncing Physical events

val error = when (it) {
is SDKNotInitializedException -> "SDKNotInitializedException: ${it.message}"
is UserNotInitializedException -> "UserNotInitializedException: ${it.message}"
is HealthConnectNotInstalledException -> "HealthConnectNotInstalledException: ${it.message}"
is DeviceNotSupportedException -> "DeviceNotSupportedException: ${it.message}"
is MissingPermissionsException -> "MissingPermissionsException: ${it.message}"
is TimeoutException -> "TimeoutException: ${it.message}"
is HttpRequestException -> "HttpRequestException: code: ${it.code} message: ${it.message}"
else -> "${it.message}"
}
}
)

Sync pending events

When you call sync_xxx_events, what happens it's that:

  1. Health data is extracted from Health Connect
  2. The extracted data is enqueued
  3. The enqueued data is uploaded to ROOK servers.
  4. If success the queued is cleared. Otherwise, the events is stored.

To retry sending those stored events call syncPendingEvents

val result = rookEventManager.syncPendingEvents()

result.fold(
{
// Pending events synced successfully
},
{
// Error syncing pending events

val error = when (it) {
is SDKNotInitializedException -> "SDKNotInitializedException: ${it.message}"
is UserNotInitializedException -> "UserNotInitializedException: ${it.message}"
is TimeoutException -> "TimeoutException: ${it.message}"
is HttpRequestException -> "HttpRequestException: code: ${it.code} message: ${it.message}"
else -> "${it.message}"
}
}
)

Recommendations

Events are collections of health data divided in intervals of 1 hour, so you can/should sync them frequently.

/**
* This function assumes that you have already checked availability and permissions.
*/
suspend fun syncEvents() {
val today = LocalDate.now()

rookEventManager.syncPhysicalEvents(today).fold(
{
// Physical events synced successfully
},
{
// Error syncing pending events
}
)

// Sync other types of events...

// Finally you can call syncPendingEvents to retry all failed uploads
// rookEventManager.syncPendingEvents() (optional)
}
  • Be cautions of syncing events an excessive amount of times, Health Connect has a daily usage limit and your app could be blocked for some hours or a full day.

Javadoc

See the complete specification of RookEventManager and RookSummaryManager in the Javadoc