Getting Started
-
Install the package
Terminal window npm i @capgo/capacitor-flashTerminal window pnpm add @capgo/capacitor-flashTerminal window yarn add @capgo/capacitor-flashTerminal window bun add @capgo/capacitor-flash -
Sync with native projects
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
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
Section titled “API Reference”isAvailable()
Section titled “isAvailable()”Checks if flashlight is available on the device.
const result = await CapacitorFlash.isAvailable();// Returns: { value: boolean }switchOn(options?)
Section titled “switchOn(options?)”Turns the flashlight on.
interface SwitchOnOptions { intensity?: number; // 0.0 to 1.0 (iOS only)}
await CapacitorFlash.switchOn({ intensity: 0.5 });switchOff()
Section titled “switchOff()”Turns the flashlight off.
await CapacitorFlash.switchOff();isSwitchedOn()
Section titled “isSwitchedOn()”Checks if the flashlight is currently on.
const result = await CapacitorFlash.isSwitchedOn();// Returns: { value: boolean }toggle()
Section titled “toggle()”Toggles the flashlight on/off.
await CapacitorFlash.toggle();Complete Example
Section titled “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
Section titled “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
Section titled “Platform Notes”- Requires iOS 10.0+
- Uses
AVCaptureDevicetorch mode - Supports intensity control (0.0 to 1.0)
Android
Section titled “Android”- Requires Android 6.0 (API 23)+
- Uses Camera2 API
- Intensity control not supported (uses default brightness)
- Supported on browsers with MediaDevices API and torch capability
- Uses the camera stream to access the device’s flashlight
- Requires user permission to access the camera
- Works best on mobile browsers (Chrome on Android)
- Desktop browsers typically return
isAvailable: false(no torch hardware) - Intensity control not supported on web