A library that provides access to the device's light sensor.
LightSensor
from expo-sensors
provides access to the device's light sensor to respond to illuminance changes.
Android Device | Android Emulator | iOS Device | iOS Simulator | Web |
---|---|---|---|---|
-
npx expo install expo-sensors
If you're installing this in a bare React Native app, you should also follow these additional installation instructions.
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, TouchableOpacity, View, Platform } from 'react-native';
import { LightSensor } from 'expo-sensors';
export default function App() {
const [{ illuminance }, setData] = useState({ illuminance: 0 });
useEffect(() => {
_toggle();
return () => {
_unsubscribe();
};
}, []);
const _toggle = () => {
if (this._subscription) {
_unsubscribe();
} else {
_subscribe();
}
};
const _subscribe = () => {
this._subscription = LightSensor.addListener(setData);
};
const _unsubscribe = () => {
this._subscription && this._subscription.remove();
this._subscription = null;
};
return (
<View style={styles.sensor}>
<Text>Light Sensor:</Text>
<Text>
Illuminance: {Platform.OS === 'android' ? `${illuminance} lx` : `Only available on Android`}
</Text>
<View style={styles.buttonContainer}>
<TouchableOpacity onPress={_toggle} style={styles.button}>
<Text>Toggle</Text>
</TouchableOpacity>
</View>
</View>
);
}
%%placeholder-start%%const styles = StyleSheet.create({ ... }); %%placeholder-end%%const styles = StyleSheet.create({
buttonContainer: {
flexDirection: 'row',
alignItems: 'stretch',
marginTop: 15,
},
button: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eee',
padding: 10,
},
sensor: {
marginTop: 45,
paddingHorizontal: 10,
},
});
import { LightSensor } from 'expo-sensors';
LightSensor
Type: Class extends default<LightSensorMeasurement>
removeSubscription(subscription)
Name | Type | Description |
---|---|---|
subscription | Subscription | A subscription to remove. |
Removes the given subscription.
Returns
void
setUpdateInterval(intervalMs)
Name | Type | Description |
---|---|---|
intervalMs | number | Desired interval in milliseconds between sensor updates.
|
Set the sensor update interval.
Returns
void
PermissionResponse
An object obtained by permissions get and request functions.
PermissionResponse Properties
Name | Type | Description |
---|---|---|
canAskAgain | boolean | Indicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission. |
expires | PermissionExpiration | Determines time when the permission expires. |
granted | boolean | A convenience boolean that indicates if the permission is granted. |
status | PermissionStatus | Determines the status of the permission. |
LightSensorMeasurement
Name | Type | Description |
---|---|---|
illuminance | number | Ambient light level registered by the device measured in lux (lx). |
PermissionExpiration
Literal Type: multiple types
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never'
number
Subscription
Name | Type | Description |
---|---|---|
remove | () => void | - |
PermissionStatus
PermissionStatus Values
UNDETERMINED
PermissionStatus.UNDETERMINED = "undetermined"
User hasn't granted or denied the permission yet.