Get device information using react-native
—
Real-time monitoring hooks for dynamic device states and event-driven updates. Perfect for React components that need to respond to device state changes automatically.
React hooks for monitoring battery state and power changes.
/**
* Hook for monitoring battery level changes
* @returns Current battery level (0.0 to 1.0) or null if loading
*/
function useBatteryLevel(): number | null;
/**
* Hook for monitoring low battery events
* @returns Battery level when low, or null
*/
function useBatteryLevelIsLow(): number | null;
/**
* Hook for monitoring power state changes
* @returns Current power state object
*/
function usePowerState(): Partial<PowerState>;
/**
* Hook for monitoring screen brightness changes (iOS only)
* @returns Current brightness level (0.0 to 1.0) or null
*/
function useBrightness(): number | null;Usage Examples:
import React from 'react';
import { View, Text } from 'react-native';
import {
useBatteryLevel,
useBatteryLevelIsLow,
usePowerState,
useBrightness
} from 'react-native-device-info';
// Battery monitor component
const BatteryMonitor = () => {
const batteryLevel = useBatteryLevel();
const lowBatteryLevel = useBatteryLevelIsLow();
const powerState = usePowerState();
const brightness = useBrightness();
const batteryPercent = batteryLevel ? Math.round(batteryLevel * 100) : 0;
const isLowBattery = lowBatteryLevel !== null;
return (
<View>
<Text>Battery: {batteryPercent}%</Text>
<Text>Charging: {powerState.batteryState === 'charging' ? 'Yes' : 'No'}</Text>
<Text>Low Power Mode: {powerState.lowPowerMode ? 'Yes' : 'No'}</Text>
{isLowBattery && (
<Text style={{ color: 'red' }}>
Low Battery Warning: {Math.round(lowBatteryLevel * 100)}%
</Text>
)}
{brightness !== null && (
<Text>Screen Brightness: {Math.round(brightness * 100)}%</Text>
)}
</View>
);
};
// Power-aware component
const PowerAwareComponent = () => {
const powerState = usePowerState();
const batteryLevel = useBatteryLevel();
// Adaptive rendering based on power state
const shouldReduceAnimations = powerState.lowPowerMode ||
(batteryLevel && batteryLevel < 0.2);
const animationDuration = shouldReduceAnimations ? 0 : 300;
return (
<View>
<Text>Power Saving Mode: {shouldReduceAnimations ? 'On' : 'Off'}</Text>
{/* Use animationDuration for animations */}
</View>
);
};Hooks for device identification and system information.
/**
* Hook for getting device name
* @returns AsyncHookResult with device name
*/
function useDeviceName(): AsyncHookResult<string>;
/**
* Hook for getting manufacturer information
* @returns AsyncHookResult with manufacturer name
*/
function useManufacturer(): AsyncHookResult<string>;
/**
* Hook for checking if device is an emulator
* @returns AsyncHookResult with emulator status
*/
function useIsEmulator(): AsyncHookResult<boolean>;
/**
* Hook for getting first install time
* @returns AsyncHookResult with install timestamp
*/
function useFirstInstallTime(): AsyncHookResult<number>;
/**
* Hook for checking system feature availability
* @param feature - System feature name to check
* @returns AsyncHookResult with feature availability
*/
function useHasSystemFeature(feature: string): AsyncHookResult<boolean>;
interface AsyncHookResult<T> {
loading: boolean;
result: T;
}Usage Examples:
import React from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
import {
useDeviceName,
useManufacturer,
useIsEmulator,
useFirstInstallTime,
useHasSystemFeature
} from 'react-native-device-info';
// Device info component
const DeviceInfoComponent = () => {
const deviceName = useDeviceName();
const manufacturer = useManufacturer();
const isEmulator = useIsEmulator();
const firstInstall = useFirstInstallTime();
const hasCamera = useHasSystemFeature('android.hardware.camera');
if (deviceName.loading || manufacturer.loading) {
return (
<View>
<ActivityIndicator />
<Text>Loading device information...</Text>
</View>
);
}
const installDate = firstInstall.result > 0 ?
new Date(firstInstall.result).toLocaleDateString() : 'Unknown';
return (
<View>
<Text>Device: {deviceName.result}</Text>
<Text>Manufacturer: {manufacturer.result}</Text>
<Text>Emulator: {isEmulator.result ? 'Yes' : 'No'}</Text>
<Text>First Install: {installDate}</Text>
<Text>Camera: {hasCamera.result ? 'Available' : 'Not Available'}</Text>
</View>
);
};
// Feature-dependent component
const CameraFeatureComponent = () => {
const hasCamera = useHasSystemFeature('android.hardware.camera');
if (hasCamera.loading) {
return <ActivityIndicator />;
}
if (!hasCamera.result) {
return <Text>Camera not available on this device</Text>;
}
return (
<View>
<Text>Camera features enabled</Text>
{/* Camera-related UI */}
</View>
);
};Hooks for monitoring audio device connections and changes.
/**
* Hook for monitoring headphone connections
* @returns AsyncHookResult with headphone connection status
*/
function useIsHeadphonesConnected(): AsyncHookResult<boolean>;
/**
* Hook for monitoring wired headphone connections
* @returns AsyncHookResult with wired headphone status
*/
function useIsWiredHeadphonesConnected(): AsyncHookResult<boolean>;
/**
* Hook for monitoring Bluetooth headphone connections
* @returns AsyncHookResult with Bluetooth headphone status
*/
function useIsBluetoothHeadphonesConnected(): AsyncHookResult<boolean>;Usage Examples:
import React, { useEffect } from 'react';
import { View, Text } from 'react-native';
import {
useIsHeadphonesConnected,
useIsWiredHeadphonesConnected,
useIsBluetoothHeadphonesConnected
} from 'react-native-device-info';
// Audio device monitor component
const AudioDeviceMonitor = () => {
const headphones = useIsHeadphonesConnected();
const wiredHeadphones = useIsWiredHeadphonesConnected();
const bluetoothHeadphones = useIsBluetoothHeadphonesConnected();
return (
<View>
<Text>Headphones: {headphones.result ? 'Connected' : 'Disconnected'}</Text>
<Text>Wired: {wiredHeadphones.result ? 'Connected' : 'Disconnected'}</Text>
<Text>Bluetooth: {bluetoothHeadphones.result ? 'Connected' : 'Disconnected'}</Text>
</View>
);
};
// Audio-aware media player
const AudioAwarePlayer = () => {
const headphones = useIsHeadphonesConnected();
const bluetoothHeadphones = useIsBluetoothHeadphonesConnected();
// Adjust audio quality based on output device
const audioQuality = headphones.result ? 'high' : 'standard';
const showVolumeWarning = !headphones.result;
useEffect(() => {
if (bluetoothHeadphones.result) {
console.log('Bluetooth headphones connected - enabling wireless features');
// Handle wireless-specific features
}
}, [bluetoothHeadphones.result]);
return (
<View>
<Text>Audio Quality: {audioQuality}</Text>
{showVolumeWarning && (
<Text style={{ color: 'orange' }}>
Warning: Playing through speakers
</Text>
)}
{bluetoothHeadphones.result && (
<Text>Bluetooth audio active</Text>
)}
</View>
);
};Advanced patterns for using device info hooks effectively.
Usage Examples:
import React, { useState, useEffect, useMemo } from 'react';
import { View, Text } from 'react-native';
import {
useBatteryLevel,
usePowerState,
useIsHeadphonesConnected,
useDeviceName,
useManufacturer
} from 'react-native-device-info';
// Performance-aware component
const PerformanceAwareComponent = () => {
const batteryLevel = useBatteryLevel();
const powerState = usePowerState();
const headphones = useIsHeadphonesConnected();
// Memoized performance profile
const performanceProfile = useMemo(() => {
const batteryOk = batteryLevel ? batteryLevel > 0.2 : false;
const isCharging = powerState.batteryState === 'charging';
const lowPowerMode = powerState.lowPowerMode;
if (isCharging || (batteryOk && !lowPowerMode)) {
return 'high';
} else if (batteryOk && !lowPowerMode) {
return 'balanced';
} else {
return 'power_saver';
}
}, [batteryLevel, powerState]);
const settings = useMemo(() => ({
animationsEnabled: performanceProfile !== 'power_saver',
highQualityAudio: headphones.result && performanceProfile === 'high',
backgroundRefreshRate: performanceProfile === 'high' ? 30 :
performanceProfile === 'balanced' ? 60 : 300
}), [performanceProfile, headphones.result]);
return (
<View>
<Text>Performance Profile: {performanceProfile}</Text>
<Text>Animations: {settings.animationsEnabled ? 'On' : 'Off'}</Text>
<Text>HQ Audio: {settings.highQualityAudio ? 'On' : 'Off'}</Text>
<Text>Refresh Rate: {settings.backgroundRefreshRate}s</Text>
</View>
);
};
// Device-specific component
const DeviceSpecificComponent = () => {
const deviceName = useDeviceName();
const manufacturer = useManufacturer();
const [deviceConfig, setDeviceConfig] = useState(null);
useEffect(() => {
if (!deviceName.loading && !manufacturer.loading) {
// Configure based on specific device
const config = {
device: deviceName.result,
manufacturer: manufacturer.result,
optimizations: getDeviceOptimizations(
manufacturer.result,
deviceName.result
)
};
setDeviceConfig(config);
}
}, [deviceName.loading, manufacturer.loading, deviceName.result, manufacturer.result]);
const getDeviceOptimizations = (mfg, device) => {
const opts = { performanceMode: 'standard' };
// Device-specific optimizations
if (mfg === 'Apple') {
opts.performanceMode = 'high';
opts.useHardwareAcceleration = true;
} else if (mfg === 'Samsung') {
opts.useCustomEffects = true;
opts.performanceMode = 'balanced';
}
return opts;
};
if (!deviceConfig) {
return <Text>Loading device configuration...</Text>;
}
return (
<View>
<Text>Device: {deviceConfig.device}</Text>
<Text>Manufacturer: {deviceConfig.manufacturer}</Text>
<Text>Performance: {deviceConfig.optimizations.performanceMode}</Text>
</View>
);
};
// Composite monitoring component
const DeviceMonitorDashboard = () => {
const batteryLevel = useBatteryLevel();
const powerState = usePowerState();
const deviceName = useDeviceName();
const headphones = useIsHeadphonesConnected();
const brightness = useBrightness();
const [alerts, setAlerts] = useState([]);
useEffect(() => {
const newAlerts = [];
if (batteryLevel && batteryLevel < 0.1) {
newAlerts.push('Critical battery level');
}
if (powerState.lowPowerMode) {
newAlerts.push('Low power mode active');
}
if (brightness && brightness > 0.9) {
newAlerts.push('High screen brightness - impacts battery');
}
setAlerts(newAlerts);
}, [batteryLevel, powerState.lowPowerMode, brightness]);
return (
<View>
<Text style={{ fontSize: 18, fontWeight: 'bold' }}>Device Status</Text>
<View style={{ marginTop: 10 }}>
<Text>Device: {deviceName.loading ? 'Loading...' : deviceName.result}</Text>
<Text>Battery: {batteryLevel ? `${Math.round(batteryLevel * 100)}%` : 'Loading...'}</Text>
<Text>Charging: {powerState.batteryState === 'charging' ? 'Yes' : 'No'}</Text>
<Text>Headphones: {headphones.result ? 'Connected' : 'Disconnected'}</Text>
{brightness !== null && (
<Text>Brightness: {Math.round(brightness * 100)}%</Text>
)}
</View>
{alerts.length > 0 && (
<View style={{ marginTop: 10 }}>
<Text style={{ color: 'red', fontWeight: 'bold' }}>Alerts:</Text>
{alerts.map((alert, index) => (
<Text key={index} style={{ color: 'red' }}>• {alert}</Text>
))}
</View>
)}
</View>
);
};
export {
PerformanceAwareComponent,
DeviceSpecificComponent,
DeviceMonitorDashboard
};Creating custom hooks for specific use cases.
Usage Examples:
import { useState, useEffect } from 'react';
import {
useBatteryLevel,
usePowerState,
useIsHeadphonesConnected
} from 'react-native-device-info';
// Custom hook for power management
const usePowerManagement = () => {
const batteryLevel = useBatteryLevel();
const powerState = usePowerState();
const [powerProfile, setPowerProfile] = useState('balanced');
useEffect(() => {
if (batteryLevel === null) return;
const isCharging = powerState.batteryState === 'charging';
const isLowPower = powerState.lowPowerMode;
if (isCharging || batteryLevel > 0.5) {
setPowerProfile('high');
} else if (batteryLevel > 0.2 && !isLowPower) {
setPowerProfile('balanced');
} else {
setPowerProfile('power_saver');
}
}, [batteryLevel, powerState]);
return {
batteryLevel,
powerState,
powerProfile,
isCharging: powerState.batteryState === 'charging',
shouldOptimize: powerProfile === 'power_saver'
};
};
// Custom hook for audio context
const useAudioContext = () => {
const headphones = useIsHeadphonesConnected();
const powerInfo = usePowerManagement();
const audioQuality = headphones.result && powerInfo.powerProfile === 'high' ?
'high' : 'standard';
const shouldShowVolumeWarning = !headphones.result;
const canUseHighQualityEffects = headphones.result &&
powerInfo.powerProfile !== 'power_saver';
return {
headphonesConnected: headphones.result,
audioQuality,
shouldShowVolumeWarning,
canUseHighQualityEffects,
loading: headphones.loading
};
};
// Usage of custom hooks
const MediaPlayerComponent = () => {
const powerInfo = usePowerManagement();
const audioContext = useAudioContext();
return (
<View>
<Text>Power Profile: {powerInfo.powerProfile}</Text>
<Text>Audio Quality: {audioContext.audioQuality}</Text>
{audioContext.shouldShowVolumeWarning && (
<Text style={{ color: 'orange' }}>Volume warning: Using speakers</Text>
)}
{powerInfo.shouldOptimize && (
<Text style={{ color: 'blue' }}>Power saving active</Text>
)}
</View>
);
};interface AsyncHookResult<T> {
loading: boolean;
result: T;
}
interface PowerState {
batteryLevel: number;
batteryState: BatteryState;
lowPowerMode: boolean;
[key: string]: any;
}
type BatteryState = 'unknown' | 'unplugged' | 'charging' | 'full';Install with Tessl CLI
npx tessl i tessl/npm-react-native-device-info