0
# Hardware Features
1
2
Hardware feature detection and capability checking for cameras, audio devices, input peripherals, and platform-specific hardware. Essential for feature-dependent applications.
3
4
## Capabilities
5
6
### Camera Detection
7
8
Check for camera availability and capabilities.
9
10
```typescript { .api }
11
/**
12
* Check if camera is present (async)
13
* @returns Promise resolving to true if camera is available
14
* @platforms Android, Windows, Web
15
*/
16
function isCameraPresent(): Promise<boolean>;
17
18
/**
19
* Check if camera is present (sync)
20
* @returns True if camera is available
21
* @platforms Android, Windows, Web
22
*/
23
function isCameraPresentSync(): boolean;
24
```
25
26
**Usage Examples:**
27
28
```typescript
29
import { isCameraPresent, isCameraPresentSync } from 'react-native-device-info';
30
31
// Check camera availability
32
const hasCamera = await isCameraPresent();
33
console.log(`Camera Available: ${hasCamera}`);
34
35
// Sync version for immediate UI decisions
36
const hasCameraSync = isCameraPresentSync();
37
38
// Feature gating
39
if (hasCamera) {
40
console.log('Enabling camera features');
41
// Show camera button
42
// Enable photo capture
43
// Enable video recording
44
} else {
45
console.log('Camera not available - hiding camera features');
46
// Hide camera UI elements
47
// Disable photo/video features
48
// Offer alternative input methods
49
}
50
51
// Platform-specific camera handling
52
if (Platform.OS === 'web') {
53
// Web camera detection
54
if (hasCamera) {
55
console.log('Web camera detected - enabling web camera features');
56
}
57
} else if (Platform.OS === 'android') {
58
// Android camera detection
59
if (hasCamera) {
60
console.log('Android camera detected');
61
}
62
}
63
```
64
65
### Audio Device Detection
66
67
Detect various types of audio devices and connections.
68
69
```typescript { .api }
70
/**
71
* Check if headphones are connected (async)
72
* @returns Promise resolving to true if headphones connected
73
* @platforms Android, iOS
74
*/
75
function isHeadphonesConnected(): Promise<boolean>;
76
77
/**
78
* Check if headphones are connected (sync)
79
* @returns True if headphones are connected
80
* @platforms Android, iOS
81
*/
82
function isHeadphonesConnectedSync(): boolean;
83
84
/**
85
* Check if wired headphones are connected (async)
86
* @returns Promise resolving to true if wired headphones connected
87
* @platforms Android, iOS
88
*/
89
function isWiredHeadphonesConnected(): Promise<boolean>;
90
91
/**
92
* Check if wired headphones are connected (sync)
93
* @returns True if wired headphones are connected
94
* @platforms Android, iOS
95
*/
96
function isWiredHeadphonesConnectedSync(): boolean;
97
98
/**
99
* Check if Bluetooth headphones are connected (async)
100
* @returns Promise resolving to true if Bluetooth headphones connected
101
* @platforms Android, iOS
102
*/
103
function isBluetoothHeadphonesConnected(): Promise<boolean>;
104
105
/**
106
* Check if Bluetooth headphones are connected (sync)
107
* @returns True if Bluetooth headphones are connected
108
* @platforms Android, iOS
109
*/
110
function isBluetoothHeadphonesConnectedSync(): boolean;
111
```
112
113
**Usage Examples:**
114
115
```typescript
116
import {
117
isHeadphonesConnected,
118
isWiredHeadphonesConnected,
119
isBluetoothHeadphonesConnected
120
} from 'react-native-device-info';
121
122
// Check audio device connections
123
const hasHeadphones = await isHeadphonesConnected();
124
const hasWiredHeadphones = await isWiredHeadphonesConnected();
125
const hasBluetoothHeadphones = await isBluetoothHeadphonesConnected();
126
127
console.log(`Headphones Connected: ${hasHeadphones}`);
128
console.log(`Wired Headphones: ${hasWiredHeadphones}`);
129
console.log(`Bluetooth Headphones: ${hasBluetoothHeadphones}`);
130
131
// Audio experience optimization
132
if (hasHeadphones) {
133
console.log('Headphones detected - enabling high-quality audio');
134
// Enable spatial audio
135
// Increase audio quality
136
// Enable noise cancellation features
137
// Show headphone-specific UI
138
} else {
139
console.log('No headphones - using speaker optimization');
140
// Optimize for speaker output
141
// Enable speaker protection
142
// Adjust EQ for speaker
143
// Show volume warnings
144
}
145
146
// Connection type specific features
147
if (hasBluetoothHeadphones) {
148
console.log('Bluetooth headphones detected');
149
// Enable wireless-specific features
150
// Monitor battery levels
151
// Handle connection drops
152
} else if (hasWiredHeadphones) {
153
console.log('Wired headphones detected');
154
// Enable high-fidelity audio
155
// Disable wireless features
156
}
157
158
// Audio routing decisions
159
const getAudioRoute = () => {
160
if (hasBluetoothHeadphones) return 'bluetooth';
161
if (hasWiredHeadphones) return 'wired';
162
return 'speaker';
163
};
164
165
const audioRoute = getAudioRoute();
166
console.log(`Audio Route: ${audioRoute}`);
167
```
168
169
### Input Device Detection (Windows)
170
171
Detect input devices on Windows platform.
172
173
```typescript { .api }
174
/**
175
* Check if mouse is connected (async)
176
* @returns Promise resolving to true if mouse is connected
177
* @platforms Windows
178
*/
179
function isMouseConnected(): Promise<boolean>;
180
181
/**
182
* Check if mouse is connected (sync)
183
* @returns True if mouse is connected
184
* @platforms Windows
185
*/
186
function isMouseConnectedSync(): boolean;
187
188
/**
189
* Check if keyboard is connected (async)
190
* @returns Promise resolving to true if keyboard is connected
191
* @platforms Windows
192
*/
193
function isKeyboardConnected(): Promise<boolean>;
194
195
/**
196
* Check if keyboard is connected (sync)
197
* @returns True if keyboard is connected
198
* @platforms Windows
199
*/
200
function isKeyboardConnectedSync(): boolean;
201
```
202
203
**Usage Examples:**
204
205
```typescript
206
import {
207
isMouseConnected,
208
isKeyboardConnected
209
} from 'react-native-device-info';
210
import { Platform } from 'react-native';
211
212
if (Platform.OS === 'windows') {
213
// Check input devices
214
const hasMouse = await isMouseConnected();
215
const hasKeyboard = await isKeyboardConnected();
216
217
console.log(`Mouse Connected: ${hasMouse}`);
218
console.log(`Keyboard Connected: ${hasKeyboard}`);
219
220
// UI adaptation based on input devices
221
if (hasMouse) {
222
console.log('Mouse detected - enabling mouse-optimized UI');
223
// Show hover effects
224
// Enable right-click menus
225
// Optimize for cursor interaction
226
}
227
228
if (hasKeyboard) {
229
console.log('Keyboard detected - enabling keyboard shortcuts');
230
// Enable keyboard navigation
231
// Show keyboard shortcuts
232
// Optimize text input
233
} else {
234
console.log('No keyboard - optimizing for touch');
235
// Optimize for on-screen keyboard
236
// Simplify text input
237
// Enable gesture controls
238
}
239
240
// Input method optimization
241
const inputMode = hasMouse && hasKeyboard ? 'desktop' : 'touch';
242
console.log(`Input Mode: ${inputMode}`);
243
}
244
```
245
246
### Security and Biometric Features
247
248
Check for security and biometric capabilities.
249
250
```typescript { .api }
251
/**
252
* Check if PIN or fingerprint is set (async)
253
* @returns Promise resolving to true if PIN/fingerprint is configured
254
* @platforms Android, iOS, Windows
255
*/
256
function isPinOrFingerprintSet(): Promise<boolean>;
257
258
/**
259
* Check if PIN or fingerprint is set (sync)
260
* @returns True if PIN/fingerprint is configured
261
* @platforms Android, iOS, Windows
262
*/
263
function isPinOrFingerprintSetSync(): boolean;
264
```
265
266
**Usage Examples:**
267
268
```typescript
269
import { isPinOrFingerprintSet } from 'react-native-device-info';
270
271
// Check security setup
272
const hasSecureAuth = await isPinOrFingerprintSet();
273
console.log(`Secure Authentication: ${hasSecureAuth}`);
274
275
// Security-dependent features
276
if (hasSecureAuth) {
277
console.log('Device has secure authentication - enabling sensitive features');
278
// Enable biometric login
279
// Allow secure data storage
280
// Enable payment features
281
// Show secure content
282
} else {
283
console.log('No secure authentication - limiting sensitive features');
284
// Disable biometric options
285
// Require additional authentication
286
// Limit sensitive data access
287
// Show security setup prompts
288
}
289
290
// App security recommendations
291
const securityRecommendations = {
292
canStoreSensitiveData: hasSecureAuth,
293
shouldPromptForSecurity: !hasSecureAuth,
294
biometricLoginAvailable: hasSecureAuth,
295
requireAdditionalAuth: !hasSecureAuth
296
};
297
298
console.log('Security Recommendations:', securityRecommendations);
299
```
300
301
### Media Type Support (Android)
302
303
Get supported media types on Android.
304
305
```typescript { .api }
306
/**
307
* Get supported media type list (async)
308
* @returns Promise resolving to array of supported media types
309
* @platforms Android
310
*/
311
function getSupportedMediaTypeList(): Promise<string[]>;
312
313
/**
314
* Get supported media type list (sync)
315
* @returns Array of supported media types
316
* @platforms Android
317
*/
318
function getSupportedMediaTypeListSync(): string[];
319
```
320
321
**Usage Examples:**
322
323
```typescript
324
import { getSupportedMediaTypeList } from 'react-native-device-info';
325
import { Platform } from 'react-native';
326
327
if (Platform.OS === 'android') {
328
// Get supported media types
329
const supportedMediaTypes = await getSupportedMediaTypeList();
330
console.log('Supported Media Types:', supportedMediaTypes);
331
332
// Check for specific media support
333
const supportsH264 = supportedMediaTypes.some(type =>
334
type.toLowerCase().includes('h264')
335
);
336
const supportsH265 = supportedMediaTypes.some(type =>
337
type.toLowerCase().includes('h265') || type.toLowerCase().includes('hevc')
338
);
339
const supportsVP9 = supportedMediaTypes.some(type =>
340
type.toLowerCase().includes('vp9')
341
);
342
343
console.log(`H.264 Support: ${supportsH264}`);
344
console.log(`H.265/HEVC Support: ${supportsH265}`);
345
console.log(`VP9 Support: ${supportsVP9}`);
346
347
// Media playback optimization
348
const getOptimalVideoCodec = () => {
349
if (supportsH265) return 'h265';
350
if (supportsVP9) return 'vp9';
351
if (supportsH264) return 'h264';
352
return 'fallback';
353
};
354
355
const optimalCodec = getOptimalVideoCodec();
356
console.log(`Optimal Video Codec: ${optimalCodec}`);
357
358
// Feature availability based on codec support
359
const canPlayHighQualityVideo = supportsH265 || supportsVP9;
360
const shouldUseHardwareAcceleration = supportsH264;
361
362
console.log(`High Quality Video: ${canPlayHighQualityVideo}`);
363
console.log(`Hardware Acceleration: ${shouldUseHardwareAcceleration}`);
364
}
365
```
366
367
### Hardware Capability Detection
368
369
Comprehensive hardware capability detection utilities.
370
371
**Usage Examples:**
372
373
```typescript
374
import {
375
isCameraPresent,
376
isHeadphonesConnected,
377
isPinOrFingerprintSet,
378
isMouseConnected,
379
isKeyboardConnected,
380
getSupportedMediaTypeList
381
} from 'react-native-device-info';
382
383
// Hardware capabilities class
384
class HardwareCapabilities {
385
static async getCapabilities() {
386
const capabilities = {
387
camera: await isCameraPresent(),
388
headphones: await isHeadphonesConnected(),
389
secureAuth: await isPinOrFingerprintSet(),
390
platform: Platform.OS
391
};
392
393
// Platform-specific capabilities
394
if (Platform.OS === 'windows') {
395
capabilities.mouse = await isMouseConnected();
396
capabilities.keyboard = await isKeyboardConnected();
397
}
398
399
if (Platform.OS === 'android') {
400
capabilities.supportedMediaTypes = await getSupportedMediaTypeList();
401
}
402
403
return capabilities;
404
}
405
406
static async getFeatureAvailability() {
407
const caps = await this.getCapabilities();
408
409
return {
410
// Camera features
411
photoCapture: caps.camera,
412
videoRecording: caps.camera,
413
qrScanning: caps.camera,
414
415
// Audio features
416
highQualityAudio: caps.headphones,
417
spatialAudio: caps.headphones,
418
419
// Security features
420
biometricAuth: caps.secureAuth,
421
secureStorage: caps.secureAuth,
422
423
// Input features (Windows)
424
preciseInput: caps.mouse || false,
425
keyboardShortcuts: caps.keyboard || false,
426
427
// Media features (Android)
428
hardwareVideoDecoding: caps.supportedMediaTypes?.length > 0 || false
429
};
430
}
431
432
static async optimizeForHardware() {
433
const features = await this.getFeatureAvailability();
434
const capabilities = await this.getCapabilities();
435
436
const optimizations = {
437
// UI optimizations
438
showCameraButton: features.photoCapture,
439
enableHoverEffects: features.preciseInput,
440
showKeyboardHints: features.keyboardShortcuts,
441
442
// Performance optimizations
443
useHardwareDecoding: features.hardwareVideoDecoding,
444
enableHighQualityAudio: features.highQualityAudio,
445
446
// Security optimizations
447
offerBiometricLogin: features.biometricAuth,
448
enableSecureFeatures: features.secureStorage
449
};
450
451
return { capabilities, features, optimizations };
452
}
453
}
454
455
// Usage
456
const hardwareInfo = await HardwareCapabilities.optimizeForHardware();
457
console.log('Hardware Optimization:', hardwareInfo);
458
459
// Apply optimizations
460
const { optimizations } = hardwareInfo;
461
462
if (optimizations.showCameraButton) {
463
console.log('Showing camera features in UI');
464
}
465
466
if (optimizations.enableHoverEffects) {
467
console.log('Enabling mouse hover effects');
468
}
469
470
if (optimizations.offerBiometricLogin) {
471
console.log('Enabling biometric authentication option');
472
}
473
```
474
475
### Hardware Event Monitoring
476
477
Monitor hardware state changes for dynamic adaptation.
478
479
**Usage Examples:**
480
481
```typescript
482
import {
483
isHeadphonesConnected,
484
isBluetoothHeadphonesConnected,
485
isWiredHeadphonesConnected
486
} from 'react-native-device-info';
487
488
// Hardware monitoring class
489
class HardwareMonitor {
490
static currentState = {};
491
492
static async checkForChanges() {
493
const newState = {
494
headphones: await isHeadphonesConnected(),
495
bluetoothHeadphones: await isBluetoothHeadphonesConnected(),
496
wiredHeadphones: await isWiredHeadphonesConnected()
497
};
498
499
// Detect changes
500
const changes = {};
501
for (const [key, value] of Object.entries(newState)) {
502
if (this.currentState[key] !== value) {
503
changes[key] = { from: this.currentState[key], to: value };
504
}
505
}
506
507
this.currentState = newState;
508
509
return { state: newState, changes };
510
}
511
512
static async startMonitoring(callback) {
513
// Initial state
514
await this.checkForChanges();
515
516
// Monitor for changes
517
setInterval(async () => {
518
const { state, changes } = await this.checkForChanges();
519
520
if (Object.keys(changes).length > 0) {
521
console.log('Hardware changes detected:', changes);
522
callback(state, changes);
523
}
524
}, 2000); // Check every 2 seconds
525
}
526
}
527
528
// Usage
529
HardwareMonitor.startMonitoring((state, changes) => {
530
console.log('Hardware state changed:', changes);
531
532
// Handle specific changes
533
if (changes.headphones) {
534
if (changes.headphones.to) {
535
console.log('Headphones connected');
536
// Switch to headphone audio profile
537
// Enable high-quality audio
538
} else {
539
console.log('Headphones disconnected');
540
// Switch to speaker audio profile
541
// Show volume warning
542
}
543
}
544
545
if (changes.bluetoothHeadphones) {
546
if (changes.bluetoothHeadphones.to) {
547
console.log('Bluetooth headphones connected');
548
// Enable wireless features
549
} else {
550
console.log('Bluetooth headphones disconnected');
551
// Handle connection loss
552
}
553
}
554
});
555
```