跳转到正文

快速入门

GitHub

你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下 prompt:

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

如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:

终端窗口
bun add @capgo/capacitor-flash
bunx cap sync
import { CapacitorFlash } from '@capgo/capacitor-flash';
import { CapacitorFlash } from '@capgo/capacitor-flash';
const { value: available } = await CapacitorFlash.isAvailable();
if (!available) {
toast('No flashlight on this device');
return;
}
await CapacitorFlash.switchOn({ intensity: 0.5 });

示例:QR 扫描仪配套工具

示例:QR 扫描仪伴侣
await CapacitorFlash.switchOn({ intensity: 1 });
const code = await scanFrame();
await CapacitorFlash.switchOff();
  • 在 finally 块中关闭:保持手电筒开启会耗电并占用相机权限。
  • Web 权限:移动端浏览器在访问手电筒前需要相机权限。
  • 亮度支持:超过硬件最大值的数值会被限制,请通过以下方式验证 isSwitchedOn().

检查设备是否支持手电筒。

import { CapacitorFlash } from '@capgo/capacitor-flash';
const { value } = await CapacitorFlash.isAvailable();
if (value) {
console.log('Flashlight is available');
}

开启手电筒。

import { CapacitorFlash } from '@capgo/capacitor-flash';
// Turn on at full brightness
await CapacitorFlash.switchOn({ intensity: 1.0 });
// Turn on at half brightness
await CapacitorFlash.switchOn({ intensity: 0.5 });

关闭手电筒。

import { CapacitorFlash } from '@capgo/capacitor-flash';
await CapacitorFlash.switchOff();

检查手电筒当前是开启还是关闭状态。

import { CapacitorFlash } from '@capgo/capacitor-flash';
const { value } = await CapacitorFlash.isSwitchedOn();
console.log('Flashlight is on:', value);

切换手电筒的开启或关闭状态。

import { CapacitorFlash } from '@capgo/capacitor-flash';
const { value } = await CapacitorFlash.toggle();
console.log('Flashlight toggled, now on:', value);

本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-flash 在“使用 @capgo/capacitor-flash”中实现原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。