Skip to main content

Initialization

Before accessing any health data, you must initialize the library.

Methods

HealthX.initialize()

Initialize the HealthX library. Must be called before any other method.

const success = await HealthX.initialize();

Returns: Promise<boolean> - true if initialization succeeded

Throws: Error if health data is not available on the device

HealthX.checkAvailability()

Check if health data is available without initializing.

const status = await HealthX.checkAvailability();

Returns: Promise<AvailabilityStatus>

interface AvailabilityStatus {
isAvailable: boolean;
reason?: 'not_supported' | 'not_installed' | 'not_authorized' | 'unknown';
message?: string;
}

HealthX.isInitialized()

Check if the library has been initialized.

const initialized = HealthX.isInitialized();

Returns: boolean

HealthX.getPlatform()

Get the current platform.

const platform = HealthX.getPlatform();
// Returns: 'ios' | 'android' | 'unsupported'

HealthX.supportsWrite()

Check if write operations are supported on the current platform.

const canWrite = HealthX.supportsWrite();
// Returns: true on Android, false on iOS

Usage Example

import HealthX from 'react-native-healthx';

async function initializeHealth() {
// Check availability first
const status = await HealthX.checkAvailability();

if (!status.isAvailable) {
switch (status.reason) {
case 'not_supported':
console.error('Health data not supported on this device');
break;
case 'not_installed':
console.error('Please install Health Connect app');
break;
default:
console.error('Health not available:', status.message);
}
return false;
}

// Initialize
try {
await HealthX.initialize();
console.log('HealthX initialized successfully');
console.log('Platform:', HealthX.getPlatform());
console.log('Write supported:', HealthX.supportsWrite());
return true;
} catch (error) {
console.error('Failed to initialize:', error);
return false;
}
}

Best Practices

Initialize Once

Initialize the library once when your app starts, not on every screen:

// App.tsx or a global health service
import { useEffect, useState } from 'react';
import HealthX from 'react-native-healthx';

export function App() {
const [healthReady, setHealthReady] = useState(false);

useEffect(() => {
async function init() {
try {
await HealthX.initialize();
setHealthReady(true);
} catch (error) {
console.error('Health init failed:', error);
}
}
init();
}, []);

if (!healthReady) {
return <LoadingScreen />;
}

return <MainApp />;
}

Check Before Access

Always verify initialization before accessing health data:

function getSleepData() {
if (!HealthX.isInitialized()) {
throw new Error('HealthX not initialized');
}

return HealthX.sleep.getSessions({ ... });
}

Handle Unavailability Gracefully

Not all devices support health data:

async function showHealthFeatures() {
const status = await HealthX.checkAvailability();

if (!status.isAvailable) {
// Hide health-related UI or show alternative
return null;
}

return <HealthDashboard />;
}