by Isaías Chávez
React Native module for accessing the device's ambient light sensor. Built with the New Architecture (TurboModules + Fabric), this module provides real-time light data on Android devices.
| Platform | Supported | Notes |
|---|---|---|
| Android | ✅ Yes | Full support via Sensor.TYPE_LIGHT |
| iOS | ❌ No | Apple does not provide public APIs for ambient light sensor access |
npm install react-native-ambient-light-moduleor
yarn add react-native-ambient-light-moduleWhile the module will compile and run on iOS, it will always return
falseforisAvailable()and reject promises with "NOT_SUPPORTED" errors. This is due to platform restrictions.
import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import * as AmbientLight from 'react-native-ambient-light-module';
function App() {
const [lux, setLux] = useState(0);
useEffect(() => {
// Check if sensor is available
AmbientLight.isAvailable().then((available) => {
if (available) {
// Start receiving updates
AmbientLight.startUpdates();
// Listen to ambient light changes
const subscription = AmbientLight.addAmbientLightListener((event) => {
setLux(event.lux);
});
return () => {
subscription.remove();
AmbientLight.stopUpdates();
};
}
});
}, []);
return (
<View>
<Text>Current Light: {lux.toFixed(2)} lux</Text>
</View>
);
}import * as AmbientLight from 'react-native-ambient-light-module';
// Set update interval to 500ms
AmbientLight.setUpdateInterval(500);
// Set smoothing factor (0.0 = no smoothing, 1.0 = maximum smoothing)
AmbientLight.setSmoothingFactor(0.7);
// Get current illuminance value
const { lux } = await AmbientLight.getCurrentIlluminance();
// Get sensor information
const sensorInfo = await AmbientLight.getSensorInfo();
console.log(sensorInfo);
// {
// vendor: "Google Inc.",
// name: "Light Sensor",
// maxRange: 65535.0,
// resolution: 1.0
// }isAvailable(): Promise<boolean>Check if the ambient light sensor is available on the device.
Returns: Promise<boolean> - true on Android devices with light sensor, false on iOS or devices without sensor.
Example:
const available = await AmbientLight.isAvailable();startUpdates(): voidStart receiving ambient light sensor updates. This will trigger the ambientLightDidUpdate event.
Note: Only works on Android. No-op on iOS.
Example:
AmbientLight.startUpdates();stopUpdates(): voidStop receiving ambient light sensor updates.
Example:
AmbientLight.stopUpdates();setUpdateInterval(ms: number): voidSet the update interval for sensor readings.
Parameters:
ms - Update interval in milliseconds (minimum: 100ms)Example:
AmbientLight.setUpdateInterval(1000); // Update every 1 secondgetCurrentIlluminance(): Promise<{ lux: number }>Get the current illuminance value from the sensor.
Returns: Promise<{ lux: number }> - Current illuminance in lux
Throws: Rejects with error on iOS or if sensor is unavailable
Example:
const result = await AmbientLight.getCurrentIlluminance();
console.log(result.lux); // e.g., 245.5setSmoothingFactor(value: number): voidSet smoothing factor for sensor data filtering using exponential moving average.
Parameters:
value - Smoothing factor between 0.0 and 1.00.0 = No smoothing (more responsive, more noise)1.0 = Maximum smoothing (less responsive, less noise)0.5Example:
AmbientLight.setSmoothingFactor(0.5);getSensorInfo(): Promise<SensorInfo>Get information about the ambient light sensor hardware.
Returns: Promise<SensorInfo> with:
interface SensorInfo {
vendor: string; // Sensor vendor name
name: string; // Sensor name
maxRange: number; // Maximum range in lux
resolution: number; // Resolution in lux
}Throws: Rejects with error on iOS or if sensor is unavailable
Example:
const info = await AmbientLight.getSensorInfo();addAmbientLightListener(listener)Add a listener for ambient light updates.
Parameters:
listener - Callback function that receives:{
lux: number; // Illuminance in lux
timestamp: number; // Timestamp in milliseconds
}Returns: Subscription object with remove() method
Example:
const subscription = AmbientLight.addAmbientLightListener((event) => {
console.log(`Lux: ${event.lux}, Time: ${event.timestamp}`);
});
// Later, remove the listener
subscription.remove();removeAllListeners(): voidRemove all ambient light event listeners.
Example:
AmbientLight.removeAllListeners();ambientLightDidUpdateEmitted when the ambient light sensor detects a change (based on update interval).
Event payload:
{
lux: number; // Current illuminance in lux
timestamp: number; // Timestamp in milliseconds
}Here are typical lux values for reference:
| Condition | Lux Value |
|---|---|
| Moonless night | < 0.01 |
| Full moon | ~0.25 |
| Dark room | ~10 |
| Office lighting | 320-500 |
| Overcast day | ~1,000 |
| Full daylight | 10,000-25,000 |
| Direct sunlight | 32,000-100,000 |
Apple does not provide public APIs to access the ambient light sensor on iOS devices. While the module will compile on iOS:
isAvailable() will always return falsegetCurrentIlluminance() and getSensorInfo() will reject with "NOT_SUPPORTED" errorstartUpdates, stopUpdates, etc.) are no-opsMIT
Made with create-react-native-library