Using @capgo/capacitor-light-sensor
Capacitor plugin for accessing the device's ambient light sensor.
Install
bun add @capgo/capacitor-light-sensor
bunx cap sync
bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.
Import
import { LightSensor } from '@capgo/capacitor-light-sensor';
API at a glance
| Method | Description |
|---|---|
isAvailable |
Check if the light sensor is available on the current device. You should always check sensor availability before attempting to use it. |
start |
Start listening to light sensor updates. This will begin sensor measurements at the specified interval. Use addListener to receive the sensor data. |
stop |
Stop listening to light sensor updates. This will stop the sensor and conserve battery. |
checkPermissions |
Check the current permission status for high sampling rate sensors. On Android 12+, the HIGH_SAMPLING_RATE_SENSORS permission is required for sensor update intervals below 200ms. |
requestPermissions |
Request permission for high sampling rate sensors. On Android 12+, this requests the HIGH_SAMPLING_RATE_SENSORS permission. |
Examples
isAvailable()
Check if the light sensor is available on the current device. You should always check sensor availability before attempting to use it.
import { LightSensor } from '@capgo/capacitor-light-sensor';
const { available } = await LightSensor.isAvailable();
start()
Start listening to light sensor updates. This will begin sensor measurements at the specified interval. Use addListener to receive the sensor data.
import { LightSensor } from '@capgo/capacitor-light-sensor';
await LightSensor.start({ updateInterval: 500 });
stop()
Stop listening to light sensor updates. This will stop the sensor and conserve battery.
import { LightSensor } from '@capgo/capacitor-light-sensor';
await LightSensor.stop();
checkPermissions()
Check the current permission status for high sampling rate sensors. On Android 12+, the HIGH_SAMPLING_RATE_SENSORS permission is required for sensor update intervals below 200ms.
import { LightSensor } from '@capgo/capacitor-light-sensor';
const status = await LightSensor.checkPermissions();
requestPermissions()
Request permission for high sampling rate sensors. On Android 12+, this requests the HIGH_SAMPLING_RATE_SENSORS permission.
import { LightSensor } from '@capgo/capacitor-light-sensor';
const status = await LightSensor.requestPermissions();
Listen to events
addListener returns a handle. Call handle.remove() when the screen unmounts, or LightSensor.removeAllListeners() to clear every listener.
Full reference
Keep going from Using @capgo/capacitor-light-sensor
If you are using Using @capgo/capacitor-light-sensor to plan dashboard and API operations, connect it with @capgo/capacitor-light-sensor for the implementation detail in @capgo/capacitor-light-sensor, Getting Started for the implementation detail in Getting Started, API Overview for the implementation detail in API Overview, Introduction for the implementation detail in Introduction, and API Keys for the implementation detail in API Keys.