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.healthSync — BWellSdk.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 ofHealthDataTypevalues to sync.window: ADateRange(from, to)specifying the period to sync.frommust not be afterto.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 aFAILEDsentinel (-1) if that specific type errored.total— sum of all successful (non-FAILED) counts.failedTypes— the subset oftypesthat returnedFAILED.
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
syncfrom a background task can cause reads to fail. AFAILEDcount with the app backgrounded is a likely sign of this. - A
FAILEDcount often means a missing permission, not a real error — confirmrequestPermissionswas 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
synctoday — 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
synccall. - Call
syncwhile your app is in the foreground. - Check
failedTypesafter 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 —
syncitself does not return the synced records.
Updated 12 days ago
