Get device information using react-native
—
Hardware feature detection and capability checking for cameras, audio devices, input peripherals, and platform-specific hardware. Essential for feature-dependent applications.
Check for camera availability and capabilities.
/**
* Check if camera is present (async)
* @returns Promise resolving to true if camera is available
* @platforms Android, Windows, Web
*/
function isCameraPresent(): Promise<boolean>;
/**
* Check if camera is present (sync)
* @returns True if camera is available
* @platforms Android, Windows, Web
*/
function isCameraPresentSync(): boolean;Usage Examples:
import { isCameraPresent, isCameraPresentSync } from 'react-native-device-info';
// Check camera availability
const hasCamera = await isCameraPresent();
console.log(`Camera Available: ${hasCamera}`);
// Sync version for immediate UI decisions
const hasCameraSync = isCameraPresentSync();
// Feature gating
if (hasCamera) {
console.log('Enabling camera features');
// Show camera button
// Enable photo capture
// Enable video recording
} else {
console.log('Camera not available - hiding camera features');
// Hide camera UI elements
// Disable photo/video features
// Offer alternative input methods
}
// Platform-specific camera handling
if (Platform.OS === 'web') {
// Web camera detection
if (hasCamera) {
console.log('Web camera detected - enabling web camera features');
}
} else if (Platform.OS === 'android') {
// Android camera detection
if (hasCamera) {
console.log('Android camera detected');
}
}Detect various types of audio devices and connections.
/**
* Check if headphones are connected (async)
* @returns Promise resolving to true if headphones connected
* @platforms Android, iOS
*/
function isHeadphonesConnected(): Promise<boolean>;
/**
* Check if headphones are connected (sync)
* @returns True if headphones are connected
* @platforms Android, iOS
*/
function isHeadphonesConnectedSync(): boolean;
/**
* Check if wired headphones are connected (async)
* @returns Promise resolving to true if wired headphones connected
* @platforms Android, iOS
*/
function isWiredHeadphonesConnected(): Promise<boolean>;
/**
* Check if wired headphones are connected (sync)
* @returns True if wired headphones are connected
* @platforms Android, iOS
*/
function isWiredHeadphonesConnectedSync(): boolean;
/**
* Check if Bluetooth headphones are connected (async)
* @returns Promise resolving to true if Bluetooth headphones connected
* @platforms Android, iOS
*/
function isBluetoothHeadphonesConnected(): Promise<boolean>;
/**
* Check if Bluetooth headphones are connected (sync)
* @returns True if Bluetooth headphones are connected
* @platforms Android, iOS
*/
function isBluetoothHeadphonesConnectedSync(): boolean;Usage Examples:
import {
isHeadphonesConnected,
isWiredHeadphonesConnected,
isBluetoothHeadphonesConnected
} from 'react-native-device-info';
// Check audio device connections
const hasHeadphones = await isHeadphonesConnected();
const hasWiredHeadphones = await isWiredHeadphonesConnected();
const hasBluetoothHeadphones = await isBluetoothHeadphonesConnected();
console.log(`Headphones Connected: ${hasHeadphones}`);
console.log(`Wired Headphones: ${hasWiredHeadphones}`);
console.log(`Bluetooth Headphones: ${hasBluetoothHeadphones}`);
// Audio experience optimization
if (hasHeadphones) {
console.log('Headphones detected - enabling high-quality audio');
// Enable spatial audio
// Increase audio quality
// Enable noise cancellation features
// Show headphone-specific UI
} else {
console.log('No headphones - using speaker optimization');
// Optimize for speaker output
// Enable speaker protection
// Adjust EQ for speaker
// Show volume warnings
}
// Connection type specific features
if (hasBluetoothHeadphones) {
console.log('Bluetooth headphones detected');
// Enable wireless-specific features
// Monitor battery levels
// Handle connection drops
} else if (hasWiredHeadphones) {
console.log('Wired headphones detected');
// Enable high-fidelity audio
// Disable wireless features
}
// Audio routing decisions
const getAudioRoute = () => {
if (hasBluetoothHeadphones) return 'bluetooth';
if (hasWiredHeadphones) return 'wired';
return 'speaker';
};
const audioRoute = getAudioRoute();
console.log(`Audio Route: ${audioRoute}`);Detect input devices on Windows platform.
/**
* Check if mouse is connected (async)
* @returns Promise resolving to true if mouse is connected
* @platforms Windows
*/
function isMouseConnected(): Promise<boolean>;
/**
* Check if mouse is connected (sync)
* @returns True if mouse is connected
* @platforms Windows
*/
function isMouseConnectedSync(): boolean;
/**
* Check if keyboard is connected (async)
* @returns Promise resolving to true if keyboard is connected
* @platforms Windows
*/
function isKeyboardConnected(): Promise<boolean>;
/**
* Check if keyboard is connected (sync)
* @returns True if keyboard is connected
* @platforms Windows
*/
function isKeyboardConnectedSync(): boolean;Usage Examples:
import {
isMouseConnected,
isKeyboardConnected
} from 'react-native-device-info';
import { Platform } from 'react-native';
if (Platform.OS === 'windows') {
// Check input devices
const hasMouse = await isMouseConnected();
const hasKeyboard = await isKeyboardConnected();
console.log(`Mouse Connected: ${hasMouse}`);
console.log(`Keyboard Connected: ${hasKeyboard}`);
// UI adaptation based on input devices
if (hasMouse) {
console.log('Mouse detected - enabling mouse-optimized UI');
// Show hover effects
// Enable right-click menus
// Optimize for cursor interaction
}
if (hasKeyboard) {
console.log('Keyboard detected - enabling keyboard shortcuts');
// Enable keyboard navigation
// Show keyboard shortcuts
// Optimize text input
} else {
console.log('No keyboard - optimizing for touch');
// Optimize for on-screen keyboard
// Simplify text input
// Enable gesture controls
}
// Input method optimization
const inputMode = hasMouse && hasKeyboard ? 'desktop' : 'touch';
console.log(`Input Mode: ${inputMode}`);
}Check for security and biometric capabilities.
/**
* Check if PIN or fingerprint is set (async)
* @returns Promise resolving to true if PIN/fingerprint is configured
* @platforms Android, iOS, Windows
*/
function isPinOrFingerprintSet(): Promise<boolean>;
/**
* Check if PIN or fingerprint is set (sync)
* @returns True if PIN/fingerprint is configured
* @platforms Android, iOS, Windows
*/
function isPinOrFingerprintSetSync(): boolean;Usage Examples:
import { isPinOrFingerprintSet } from 'react-native-device-info';
// Check security setup
const hasSecureAuth = await isPinOrFingerprintSet();
console.log(`Secure Authentication: ${hasSecureAuth}`);
// Security-dependent features
if (hasSecureAuth) {
console.log('Device has secure authentication - enabling sensitive features');
// Enable biometric login
// Allow secure data storage
// Enable payment features
// Show secure content
} else {
console.log('No secure authentication - limiting sensitive features');
// Disable biometric options
// Require additional authentication
// Limit sensitive data access
// Show security setup prompts
}
// App security recommendations
const securityRecommendations = {
canStoreSensitiveData: hasSecureAuth,
shouldPromptForSecurity: !hasSecureAuth,
biometricLoginAvailable: hasSecureAuth,
requireAdditionalAuth: !hasSecureAuth
};
console.log('Security Recommendations:', securityRecommendations);Get supported media types on Android.
/**
* Get supported media type list (async)
* @returns Promise resolving to array of supported media types
* @platforms Android
*/
function getSupportedMediaTypeList(): Promise<string[]>;
/**
* Get supported media type list (sync)
* @returns Array of supported media types
* @platforms Android
*/
function getSupportedMediaTypeListSync(): string[];Usage Examples:
import { getSupportedMediaTypeList } from 'react-native-device-info';
import { Platform } from 'react-native';
if (Platform.OS === 'android') {
// Get supported media types
const supportedMediaTypes = await getSupportedMediaTypeList();
console.log('Supported Media Types:', supportedMediaTypes);
// Check for specific media support
const supportsH264 = supportedMediaTypes.some(type =>
type.toLowerCase().includes('h264')
);
const supportsH265 = supportedMediaTypes.some(type =>
type.toLowerCase().includes('h265') || type.toLowerCase().includes('hevc')
);
const supportsVP9 = supportedMediaTypes.some(type =>
type.toLowerCase().includes('vp9')
);
console.log(`H.264 Support: ${supportsH264}`);
console.log(`H.265/HEVC Support: ${supportsH265}`);
console.log(`VP9 Support: ${supportsVP9}`);
// Media playback optimization
const getOptimalVideoCodec = () => {
if (supportsH265) return 'h265';
if (supportsVP9) return 'vp9';
if (supportsH264) return 'h264';
return 'fallback';
};
const optimalCodec = getOptimalVideoCodec();
console.log(`Optimal Video Codec: ${optimalCodec}`);
// Feature availability based on codec support
const canPlayHighQualityVideo = supportsH265 || supportsVP9;
const shouldUseHardwareAcceleration = supportsH264;
console.log(`High Quality Video: ${canPlayHighQualityVideo}`);
console.log(`Hardware Acceleration: ${shouldUseHardwareAcceleration}`);
}Comprehensive hardware capability detection utilities.
Usage Examples:
import {
isCameraPresent,
isHeadphonesConnected,
isPinOrFingerprintSet,
isMouseConnected,
isKeyboardConnected,
getSupportedMediaTypeList
} from 'react-native-device-info';
// Hardware capabilities class
class HardwareCapabilities {
static async getCapabilities() {
const capabilities = {
camera: await isCameraPresent(),
headphones: await isHeadphonesConnected(),
secureAuth: await isPinOrFingerprintSet(),
platform: Platform.OS
};
// Platform-specific capabilities
if (Platform.OS === 'windows') {
capabilities.mouse = await isMouseConnected();
capabilities.keyboard = await isKeyboardConnected();
}
if (Platform.OS === 'android') {
capabilities.supportedMediaTypes = await getSupportedMediaTypeList();
}
return capabilities;
}
static async getFeatureAvailability() {
const caps = await this.getCapabilities();
return {
// Camera features
photoCapture: caps.camera,
videoRecording: caps.camera,
qrScanning: caps.camera,
// Audio features
highQualityAudio: caps.headphones,
spatialAudio: caps.headphones,
// Security features
biometricAuth: caps.secureAuth,
secureStorage: caps.secureAuth,
// Input features (Windows)
preciseInput: caps.mouse || false,
keyboardShortcuts: caps.keyboard || false,
// Media features (Android)
hardwareVideoDecoding: caps.supportedMediaTypes?.length > 0 || false
};
}
static async optimizeForHardware() {
const features = await this.getFeatureAvailability();
const capabilities = await this.getCapabilities();
const optimizations = {
// UI optimizations
showCameraButton: features.photoCapture,
enableHoverEffects: features.preciseInput,
showKeyboardHints: features.keyboardShortcuts,
// Performance optimizations
useHardwareDecoding: features.hardwareVideoDecoding,
enableHighQualityAudio: features.highQualityAudio,
// Security optimizations
offerBiometricLogin: features.biometricAuth,
enableSecureFeatures: features.secureStorage
};
return { capabilities, features, optimizations };
}
}
// Usage
const hardwareInfo = await HardwareCapabilities.optimizeForHardware();
console.log('Hardware Optimization:', hardwareInfo);
// Apply optimizations
const { optimizations } = hardwareInfo;
if (optimizations.showCameraButton) {
console.log('Showing camera features in UI');
}
if (optimizations.enableHoverEffects) {
console.log('Enabling mouse hover effects');
}
if (optimizations.offerBiometricLogin) {
console.log('Enabling biometric authentication option');
}Monitor hardware state changes for dynamic adaptation.
Usage Examples:
import {
isHeadphonesConnected,
isBluetoothHeadphonesConnected,
isWiredHeadphonesConnected
} from 'react-native-device-info';
// Hardware monitoring class
class HardwareMonitor {
static currentState = {};
static async checkForChanges() {
const newState = {
headphones: await isHeadphonesConnected(),
bluetoothHeadphones: await isBluetoothHeadphonesConnected(),
wiredHeadphones: await isWiredHeadphonesConnected()
};
// Detect changes
const changes = {};
for (const [key, value] of Object.entries(newState)) {
if (this.currentState[key] !== value) {
changes[key] = { from: this.currentState[key], to: value };
}
}
this.currentState = newState;
return { state: newState, changes };
}
static async startMonitoring(callback) {
// Initial state
await this.checkForChanges();
// Monitor for changes
setInterval(async () => {
const { state, changes } = await this.checkForChanges();
if (Object.keys(changes).length > 0) {
console.log('Hardware changes detected:', changes);
callback(state, changes);
}
}, 2000); // Check every 2 seconds
}
}
// Usage
HardwareMonitor.startMonitoring((state, changes) => {
console.log('Hardware state changed:', changes);
// Handle specific changes
if (changes.headphones) {
if (changes.headphones.to) {
console.log('Headphones connected');
// Switch to headphone audio profile
// Enable high-quality audio
} else {
console.log('Headphones disconnected');
// Switch to speaker audio profile
// Show volume warning
}
}
if (changes.bluetoothHeadphones) {
if (changes.bluetoothHeadphones.to) {
console.log('Bluetooth headphones connected');
// Enable wireless features
} else {
console.log('Bluetooth headphones disconnected');
// Handle connection loss
}
}
});Install with Tessl CLI
npx tessl i tessl/npm-react-native-device-info