This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo LightSensor iconExpo LightSensor

提供对设备光传感器的访问的库。

Android
Included in Expo Go
Recommended version:
~58.0.3

expo-sensors 的 LightSensor 提供对设备光传感器的访问,以响应照度变化。

安装

🌐 Installation

Terminal
- npx expo install expo-sensors
- yarn expo install expo-sensors
- pnpm expo install expo-sensors
- bun expo install expo-sensors

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

Basic Light Sensor usage
import { 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 }); const [subscription, setSubscription] = useState(null); const toggle = () => { if (subscription) { unsubscribe(); } else { subscribe(); } }; const subscribe = () => { setSubscription( LightSensor.addListener(sensorData => { setData(sensorData); }) ); }; const unsubscribe = () => { subscription && subscription.remove(); setSubscription(null); }; useEffect(() => { subscribe(); return () => unsubscribe(); }, []); 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> ); } const styles = StyleSheet.create({ sensor: { flex: 1, justifyContent: 'center', alignItems: 'center', paddingHorizontal: 10, }, buttonContainer: { flexDirection: 'row', alignItems: 'stretch', marginTop: 15, }, button: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#eee', padding: 10, }, });

应用接口

🌐 API

import { LightSensor } from 'expo-sensors';

No API data file found, sorry!