Skip to main content

AndroidHealthSync

A React component that manages the synchronization of health data from Android Health Connect to a FHIR-compliant backend, providing real-time progress tracking and network-aware syncing.

Overview

The AndroidHealthSync component handles the complex process of reading health data from Android Health Connect and synchronizing it with a FHIR backend. It provides intelligent syncing with network awareness, automatic retry mechanisms, and real-time progress updates.

Features

  • FHIR Integration: Converts Health Connect data to FHIR Observation resources
  • Network Awareness: Automatically pauses/resumes sync based on network conditions
  • Progress Tracking: Real-time progress updates for each measurement type
  • Chunked Processing: Handles large datasets efficiently through chunked synchronization
  • Error Handling: Comprehensive error handling with callback notifications
  • Background Sync: Maintains sync operations in the background
  • Automatic Cleanup: Properly stops sync operations on component unmount

Basic Example

import { ObservationCode } from "@ovok/core";
import { AndroidHealthSyncProps, DataSync } from "@ovok/native";
import React from "react";

const dataToSync: AndroidHealthSyncProps["dataToSync"] = {
"body-temperature": {
typeIdentifiers: [
{
typeIdentifier: "BodyTemperature",
code: ObservationCode.BODY_TEMPERATURE,
},
],
minDate: new Date("2025-01-01"),
},
};

const HealthDataSync = () => {
// This component is wrapped in the AndroidHealthConnectAuthorizationProvider
// to handle the authorization process
return (
<DataSync.AndroidHealthConnectAuthorizationProvider
readIdentifiers={["BodyTemperature"]}
>
{/* You have to be authorized to sync the health data */}
<DataSync.AndroidHealthSync dataToSync={dataToSync} />
</DataSync.AndroidHealthConnectAuthorizationProvider>
);
};

export default HealthDataSync;

Props

PropTypeDefaultDescription
dataToSyncRecord<HealthSyncKeys, { typeIdentifiers: TypeIdentifier[], minDate?: Date }>requiredConfiguration of data types to sync with their identifiers
chunkSizenumber5000Number of samples to process in each sync batch
onError(error: Bundle<Resource> | Error | null) => voidundefinedCallback for sync errors
onProgress(progress: Record<MeasurementTypeKey, SyncProgress>) => voidundefinedCallback for real-time progress updates

Data Configuration

The dataToSync prop defines which health data types to synchronize:

type DataToSync = {
[key in HealthSyncKeys]?: {
typeIdentifiers: {
typeIdentifier: RecordType; // Health Connect record type
code: ObservationCode; // FHIR observation code
}[];
minDate?: Date; // Optional start date for sync
};
};

Network Behavior

The component automatically manages sync operations based on network conditions:

  • WiFi Connection: Full sync operations enabled
  • Mobile Data: Sync operations paused (configurable)
  • No Connection: All sync operations paused
  • Connection Restored: Sync automatically resumes

Progress Tracking

The component provides detailed progress information through the onProgress callback:

interface SyncProgress {
measurementTypeKey: MeasurementTypeKey;
status: "started" | "completed" | "idle" | "syncing" | "paused";
progress?: {
done: number; // Number of samples processed
total: number; // Total samples to process
};
}

Progress States

  • idle: Sync not yet started
  • started: Sync initialization in progress
  • syncing: Actively processing data
  • paused: Sync paused (usually due to network conditions)
  • completed: All data successfully synced

Error Handling

The component handles various error scenarios:

Network Errors

const handleSyncError = (error: Error) => {
if (error.message.includes("network")) {
// Network-related error - sync will auto-retry when connection restored
showNotification("Sync paused - waiting for network connection");
}
};

Performance Considerations

  • Chunked Processing: Processes data in configurable chunks to prevent memory issues
  • Background Processing: Runs sync operations on background threads
  • Network Optimization: Only syncs on WiFi by default to preserve mobile data
  • Memory Management: Automatically cleans up resources on component unmount
  • Debounced Updates: Progress updates are debounced to prevent excessive re-renders

Platform Requirements

  • Android Only: Exclusive to Android devices
  • Health Connect: Requires Android Health Connect app
  • Network: Requires internet connectivity for FHIR backend
  • Permissions: Requires appropriate Health Connect permissions

Dependencies

  • @ovok/core: FHIR client and patient management
  • react-native-health-connect: Android Health Connect integration
  • @react-native-community/netinfo: Network status monitoring