Skip to main content
@capgo/capacitor-light-sensor Device APIs Open source

Light Sensor Capacitor plugin

Access the ambient light sensor to measure illuminance levels in lux with real-time updates

Install

bun add @capgo/capacitor-light-sensor bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-light-sensor
  • pnpm add @capgo/capacitor-light-sensor
  • yarn add @capgo/capacitor-light-sensor

Guide

How to use Light Sensor in Capacitor

Test on device

Download the Capgo app, then scan the QR code.

Light Sensor plugin preview QR code

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.

FAQ

Light Sensor plugin FAQ

How do I install the Light Sensor plugin in a Capacitor app?

Run "bun add @capgo/capacitor-light-sensor" (or "npm install @capgo/capacitor-light-sensor"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-light-sensor" in your app code.

Does @capgo/capacitor-light-sensor work with React, Vue and Angular?

Yes. Light Sensor is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-light-sensor support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-light-sensor free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-light-sensor/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses Light Sensor without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship Light Sensor changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo