开始使用
复制一个设置提示,包含安装步骤和该插件的完整Markdown指南。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-light-sensor`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/light-sensor/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
安装可以使用我们的AI辅助设置来安装插件。将Capgo技能添加到您的AI工具中,使用以下命令:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-light-sensor` plugin in my project.如果您更喜欢手动设置,请按照以下命令安装插件,并遵循以下平台特定的说明:
bun add @capgo/capacitor-light-sensorbunx cap syncimport { LightSensor } from '@capgo/capacitor-light-sensor';API概述
API 简介isAvailable
检查当前设备上的光感器是否可用。
您应该始终在尝试使用它之前检查传感器的可用性。Copy to clipboard
import { LightSensor } from '@capgo/capacitor-light-sensor';
const { available } = await LightSensor.isAvailable();start
开始开始监听光线传感器更新。
这将在指定间隔开始传感器测量。
使用 addListener 接收传感器数据。
import { LightSensor } from '@capgo/capacitor-light-sensor';
await LightSensor.start({ updateInterval: 500 });stop
停止停止监听光线传感器更新。 这将停止传感器并节省电池。
import { LightSensor } from '@capgo/capacitor-light-sensor';
await LightSensor.stop();checkPermissions
检查检查当前高采样率传感器的权限状态。 在 Android 12+ 上,需要 HIGH_SAMPLING_RATE_SENSORS 权限 才能在 200ms 以下的传感器更新间隔下更新传感器。
import { LightSensor } from '@capgo/capacitor-light-sensor';
const status = await LightSensor.checkPermissions();requestPermissions
请求申请高采样率传感器权限。 在 Android 12+ 中,这会请求 HIGH_SAMPLING_RATE_SENSORS 权限。
import { LightSensor } from '@capgo/capacitor-light-sensor';
const status = await LightSensor.requestPermissions();类型参考
类型参考IsAvailableResult
类型参考类型参考
export interface IsAvailableResult { /** * Whether the light sensor is available on this device. * Always false on iOS as the light sensor API is not available. * * @since 0.0.1 */ available: boolean;}StartOptions
类型参考类型参考
export interface StartOptions { /** * The 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. * * @default 200 * @since 0.0.1 */ updateInterval?: number;}LightSensorCallback
类型参考类型参考
export type LightSensorCallback = (measurement: LightSensorMeasurement) => void;PermissionStatus
权限状态权限请求或检查结果
export interface PermissionStatus { /** * Whether the high sampling rate sensor permission is granted. * On Android 12+, this permission is required for update intervals below 200ms. * * @since 0.0.1 */ highSamplingRate: 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';}VersionResult
版本结果光线传感器测量
export interface VersionResult { /** * The current version of the plugin. * * @since 0.0.1 */ version: string;}LightSensorMeasurement
真实来源真实来源
export interface LightSensorMeasurement { /** * Ambient light level in lux (lx). * * @since 0.0.1 */ illuminance: number;
/** * Timestamp of the measurement in seconds since epoch. * * @since 0.0.1 */ timestamp: number;}Section titled “PermissionStatus”
Section titled “VersionResult”本页面由插件生成。 src/definitions.ts重新同步时,公共API上游发生变化。
继续从Getting Started
标题为“继续从Getting Started”如果您正在使用 开始使用 来规划仪表板和API操作,连接它到 使用@capgo/capacitor-light-sensor 为native能力在使用@capgo/capacitor-light-sensor 中API概述 为API概述 Introduction 为《介绍》中实现细节 API 键 为《API 键》中实现细节,并 设备 为《设备》中实现细节。