Getting Started
Este contenido aún no está disponible en tu idioma.
-
Install the package
Ventana de terminal npm i @capgo/capacitor-flashVentana de terminal pnpm add @capgo/capacitor-flashVentana de terminal yarn add @capgo/capacitor-flashVentana de terminal bun add @capgo/capacitor-flash -
Sync with native projects
Ventana de terminal npx cap syncVentana de terminal pnpm cap syncVentana de terminal yarn cap syncVentana de terminal bunx cap sync
Usage
Import the plugin and use its methods to control the flashlight:
import { CapacitorFlash } from '@capgo/capacitor-flash';
// Check if flashlight is availableconst checkFlashlight = async () => { const { value } = await CapacitorFlash.isAvailable(); console.log('Flashlight available:', value);};
// Turn on flashlightconst turnOn = async () => { await CapacitorFlash.switchOn();};
// Turn off flashlightconst turnOff = async () => { await CapacitorFlash.switchOff();};
// Check if flashlight is onconst checkStatus = async () => { const { value } = await CapacitorFlash.isSwitchedOn(); console.log('Flashlight is on:', value);};
// Toggle flashlightconst toggle = async () => { await CapacitorFlash.toggle();};
API Reference
isAvailable()
Checks if flashlight is available on the device.
const result = await CapacitorFlash.isAvailable();// Returns: { value: boolean }
switchOn(options?)
Turns the flashlight on.
interface SwitchOnOptions { intensity?: number; // 0.0 to 1.0 (iOS only)}
await CapacitorFlash.switchOn({ intensity: 0.5 });
switchOff()
Turns the flashlight off.
await CapacitorFlash.switchOff();
isSwitchedOn()
Checks if the flashlight is currently on.
const result = await CapacitorFlash.isSwitchedOn();// Returns: { value: boolean }
toggle()
Toggles the flashlight on/off.
await CapacitorFlash.toggle();
Complete Example
import { CapacitorFlash } from '@capgo/capacitor-flash';
export class FlashlightService { private isOn = false;
async init() { const { value } = await CapacitorFlash.isAvailable(); if (!value) { throw new Error('Flashlight not available on this device'); } }
async toggle() { if (this.isOn) { await CapacitorFlash.switchOff(); } else { await CapacitorFlash.switchOn(); } this.isOn = !this.isOn; }
async turnOn(intensity?: number) { await CapacitorFlash.switchOn({ intensity }); this.isOn = true; }
async turnOff() { await CapacitorFlash.switchOff(); this.isOn = false; }
async strobe(intervalMs: number = 100, duration: number = 3000) { const endTime = Date.now() + duration;
while (Date.now() < endTime) { await CapacitorFlash.toggle(); await new Promise(resolve => setTimeout(resolve, intervalMs)); }
// Ensure flashlight is off after strobe await CapacitorFlash.switchOff(); this.isOn = false; }}
Best Practices
-
Check availability first Always verify flashlight availability before using it:
const { value } = await CapacitorFlash.isAvailable();if (!value) {// Show alternative UI or disable flashlight features} -
Handle errors gracefully
try {await CapacitorFlash.switchOn();} catch (error) {console.error('Failed to turn on flashlight:', error);} -
Turn off when not needed Always turn off the flashlight when your app goes to background or when the feature is no longer needed to save battery.
-
Avoid rapid toggling Implement debouncing to prevent rapid on/off switching which could damage the LED or drain battery.
Platform Notes
iOS
- Requires iOS 10.0+
- Uses
AVCaptureDevice
torch mode - Supports intensity control (0.0 to 1.0)
Android
- Requires Android 6.0 (API 23)+
- Uses Camera2 API
- Intensity control not supported (uses default brightness)
Web
- Not supported on web platform
- Will return
isAvailable: false