Sync

b.well Health SDK for Android

Syncing Health Data

Overview

The sync method in the b.well SDK reads health data of the specified types, within a given date range, from the connected on-device health source. With the registered on-device adapter, that read is what feeds the adapter's own pipeline into b.well as a side effect — there is no separate, explicit "upload" call. sync itself returns per-type counts of records found, never the records themselves. To read the processed data back, use Retrieve Device Metrics Groups after syncing.

sync is called on BWellSdk.healthSyncBWellSdk.healthSync.sync(...) — the same object every other Health Sync method (connect, disconnect, requestPermissions) lives on.

Method Signature

suspend fun sync(types: Set<HealthDataType>, window: DateRange, sourceId: String? = null): BWellResult<SyncCounts>
  • types: The set of HealthDataType values to sync.
  • window: A DateRange(from, to) specifying the period to sync. from must not be after to.
  • sourceId: identifies which registered on-device source to sync from. In practice, omit this — there's currently only one supported adapter, auto-selected.
  • BWellResult<SyncCounts>:
    • perType: Map<HealthDataType, Int> — record count found for each requested type, or a FAILED sentinel (-1) if that specific type errored.
    • total — sum of all successful (non-FAILED) counts.
    • failedTypes — the subset of types that returned FAILED.

Under the Hood

For each requested type, sync requires an active session (from a prior connect), then makes one read call to the on-device store for that type. Each type is read independently — one type failing doesn't stop the others from being read.

Known Limitations

  • Must run in the foreground. Reading from Android Health Connect requires the app to be in the foreground; calling sync from a background task can cause reads to fail. A FAILED count with the app backgrounded is a likely sign of this.
  • A FAILED count often means a missing permission, not a real error — confirm requestPermissions was called and granted for that type before troubleshooting further.
  • Continuous glucose monitor (CGM) data is not included. Blood glucose from CGM devices (e.g. Abbott, Dexcom) is not read by sync today — only standard blood glucose measurements are covered.

Example Usage

val now = Instant.now()
val result = BWellSdk.healthSync.sync(
    HealthDataType.entries.toSet(),
    DateRange(now.minus(30, ChronoUnit.DAYS), now),
)
if (result.success() && result is BWellResult.SingleResource) {
    val counts = result.data
}

Best Practices

  • Request permissions for a type before including it in a sync call.
  • Call sync while your app is in the foreground.
  • Check failedTypes after a sync and cross-reference against granted permissions before assuming a data or connectivity issue.
  • Poll Retrieve Device Metrics Groups after syncing to confirm data was processed — sync itself does not return the synced records.

Did this page help you?