@capgo/capacitor-light-sensor

中文文档
8.1.23发布于 2026/10/15MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4.1万

Capacitor plugin for accessing the device light sensor (Android only)

原始使用文档
文档目录25 节
  1. ➡️ Get started for free
  2. Key features
  3. Why Capacitor Light Sensor?
  4. Platform Support
  5. Compatibility
  6. Installation
  7. Requirements
  8. Android
  9. High Sampling Rate (Android 12+)
  10. Usage
  11. Light Level Reference
  12. API
  13. isAvailable()
  14. start(...)
  15. stop()
  16. addListener('lightSensorChange', ...)
  17. removeAllListeners()
  18. checkPermissions()
  19. requestPermissions()
  20. getPluginVersion()
  21. Interfaces
  22. Type Aliases
  23. Contributing
  24. License
  25. Credits

@capgo/capacitor-light-sensor

Read the ambient light level from the device sensor in your Capacitor app, to adapt themes, brightness or camera hints to the room.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-light-sensor in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-light-sensor to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-light-sensor for Capacitor apps

Key features

  • Live readings: start() streams lux values through the lightSensorChange event.
  • Sampling control: pass an update interval to start().
  • Availability: isAvailable() checks for a light sensor first.
  • Stop: stop() ends updates to save battery.
  • Permissions: checkPermissions() and requestPermissions() exist for API parity. Both always report highSamplingRate as granted.
  • Platforms: Android. Android only. iOS has no public ambient light sensor API, and web is not available.

Why Capacitor Light Sensor?

  • Real Ambient Light Data: Get accurate light level readings in lux
  • Efficient Sensor Access: Uses native Android sensor APIs for optimal performance
  • Configurable Update Intervals: Control how often you receive sensor updates
  • Battery Conscious: Start and stop the sensor as needed to conserve battery
  • TypeScript Support: Full type definitions for a great developer experience

Platform Support

PlatformSupport
Android✅ Full support via TYPE_LIGHT sensor
iOS❌ Not available (no public API)
Web❌ Not available

Compatibility

Plugin versionCapacitor compatibilityMaintained
v8.*.*v8.*.*✅
v7.*.*v7.*.*On demand
v6.*.*v6.*.*❌
v5.*.*v5.*.*❌

Note: The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.

Installation

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-light-sensor` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capgo/capacitor-light-sensor
npx cap sync

Requirements

Android

  • Minimum SDK: 24 (Android 7.0)
  • Target SDK: 36
  • Device must have a light sensor (most Android phones do)

High Sampling Rate (Android 12+)

For update intervals below 200ms on Android 12 and above, add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />

Usage

import { LightSensor } from '@capgo/capacitor-light-sensor';

// Check if sensor is available
const { available } = await LightSensor.isAvailable();

if (available) {
  // Start listening with 500ms update interval
  await LightSensor.start({ updateInterval: 500 });

  // Add listener for sensor data
  const handle = await LightSensor.addListener('lightSensorChange', (data) => {
    console.log(`Light level: ${data.illuminance} lux`);
    console.log(`Timestamp: ${data.timestamp}`);
  });

  // Later, stop the sensor
  await LightSensor.stop();
  await handle.remove();
}

Light Level Reference

Lux ValueCondition
0.0001Moonless, overcast night
0.27-1Full moon on a clear night
3.4Dark limit of civil twilight
50Family living room
80Office hallway
100Very dark overcast day
400Sunrise/sunset on clear day
1,000Overcast day
10,000-25,000Full daylight (indirect)
32,000-100,000Direct sunlight

API

Capacitor plugin for accessing the device's ambient light sensor.

isAvailable()

isAvailable() => Promise<IsAvailableResult>

Check if the light sensor is available on the current device. You should always check sensor availability before attempting to use it.

Returns: Promise<IsAvailableResult>

Since: 0.0.1


start(...)

start(options?: StartOptions | undefined) => Promise<void>

Start listening to light sensor updates. This will begin sensor measurements at the specified interval. Use addListener to receive the sensor data.

ParamTypeDescription
optionsStartOptions- Configuration options for the sensor

Since: 0.0.1


stop()

stop() => Promise<void>

Stop listening to light sensor updates. This will stop the sensor and conserve battery.

Since: 0.0.1


addListener('lightSensorChange', ...)

addListener(eventName: 'lightSensorChange', listenerFunc: LightSensorCallback) => Promise<PluginListenerHandle>

Add a listener for light sensor change events. The listener will be called whenever new sensor data is available.

ParamTypeDescription
eventName'lightSensorChange'- Must be 'lightSensorChange'
listenerFuncLightSensorCallback- Callback function to receive sensor data

Returns: Promise<PluginListenerHandle>

Since: 0.0.1


removeAllListeners()

removeAllListeners() => Promise<void>

Remove all listeners for light sensor events.

Since: 0.0.1


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

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.

Returns: Promise<PermissionStatus>

Since: 0.0.1


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

Request permission for high sampling rate sensors. On Android 12+, this requests the HIGH_SAMPLING_RATE_SENSORS permission.

Returns: Promise<PermissionStatus>

Since: 0.0.1


getPluginVersion()

getPluginVersion() => Promise<VersionResult>

Get the current version of the plugin.

Returns: Promise<VersionResult>

Since: 0.0.1


Interfaces

IsAvailableResult

Result indicating whether the sensor is available.

PropTypeDescriptionSince
availablebooleanWhether the light sensor is available on this device. Always false on iOS as the light sensor API is not available.0.0.1

StartOptions

Options for starting the light sensor listener.

PropTypeDescriptionDefaultSince
updateIntervalnumberThe desired interval between sensor updates in milliseconds. On Android 12+, there's a minimum interval of 200ms unless the app has the HIGH_SAMPLING_RATE_SENSORS permission.2000.0.1

PluginListenerHandle

PropType
remove() => Promise<void>

LightSensorMeasurement

A single light sensor measurement.

PropTypeDescriptionSince
illuminancenumberAmbient light level in lux (lx).0.0.1
timestampnumberTimestamp of the measurement in seconds since epoch.0.0.1

PermissionStatus

Result of a permission request or check.

PropTypeDescriptionSince
highSamplingRate'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'Whether the high sampling rate sensor permission is granted. On Android 12+, this permission is required for update intervals below 200ms.0.0.1

VersionResult

Plugin version information.

PropTypeDescriptionSince
versionstringThe current version of the plugin.0.0.1

Type Aliases

LightSensorCallback

Callback function for light sensor updates.

(measurement: LightSensorMeasurement): void

Contributing

See CONTRIBUTING.md for details on how to contribute to this plugin.

License

MPL-2.0

Credits

This SDK has been inspired by Expo light sensor.

版本与发布记录

@capgo/capacitor-light-sensor 共发布 24 个版本,首次发布于 2026/01/05;过去 12 个月发布了 24 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.23latest2026/10/1525 天前
8.1.222026/10/1327 天前
8.1.212026/10/091 个月前
8.1.192026/10/081 个月前
8.1.202026/10/081 个月前
8.1.182026/09/161 个月前

关于 @capgo/capacitor-light-sensor 的常见问题

@capgo/capacitor-light-sensor 是什么?

Capacitor plugin for accessing the device light sensor (Android only)

如何安装 @capgo/capacitor-light-sensor?

在项目目录运行 npm install @capgo/capacitor-light-sensor,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capgo/capacitor-light-sensor 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capgo/capacitor-light-sensor 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capgo/capacitor-light-sensor 可以商用吗?

@capgo/capacitor-light-sensor 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-light-sensor 的下载量有多少?

根据 npm 官方下载统计,@capgo/capacitor-light-sensor 在 2026/10/09 – 2026/11/07 这 30 天内被下载 41,242 次,日均约 1,375 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capgo/capacitor-light-sensor 还在维护吗?

最新版本 8.1.23 发布于 2026/10/15(25 天前);至今共发布 24 个版本,其中过去 12 个月发布了 24 个;首次发布于 2026/01/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。