Passer au contenu

Commencer

  1. Installer the package

    Fenêtre de terminal
    npm i @capgo/capacitor-flash
  2. Synchroniser with Natif projects

    Fenêtre de terminal
    npx cap sync

Importer the plugin and use its methods to control the flashlight:

import { CapacitorFlash } from '@capgo/capacitor-flash';
// Check if flashlight is available
const checkFlashlight = async () => {
const { value } = await CapacitorFlash.isAvailable();
console.log('Flashlight available:', value);
};
// Turn on flashlight
const turnOn = async () => {
await CapacitorFlash.switchOn();
};
// Turn off flashlight
const turnOff = async () => {
await CapacitorFlash.switchOff();
};
// Check if flashlight is on
const checkStatus = async () => {
const { value } = await CapacitorFlash.isSwitchedOn();
console.log('Flashlight is on:', value);
};
// Toggle flashlight
const toggle = async () => {
await CapacitorFlash.toggle();
};

Checks if flashlight is Disponible on the Appareil.

const result = await CapacitorFlash.isAvailable();
// Returns: { value: boolean }

Turns the flashlight on.

interface SwitchOnOptions {
intensity?: number; // 0.0 to 1.0 (iOS only)
}
await CapacitorFlash.switchOn({ intensity: 0.5 });

Turns the flashlight off.

await CapacitorFlash.switchOff();

Checks if the flashlight is currently on.

const result = await CapacitorFlash.isSwitchedOn();
// Returns: { value: boolean }

Toggles the flashlight on/off.

await CapacitorFlash.toggle();
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;
}
}
  1. Vérifier availability first Always verify flashlight availability before using it:

    const { value } = await CapacitorFlash.isAvailable();
    if (!value) {
    // Show alternative UI or disable flashlight features
    }
  2. Handle errors gracefully

    try {
    await CapacitorFlash.switchOn();
    } catch (error) {
    console.error('Failed to turn on flashlight:', error);
    }
  3. Turn off when not needed Always turn off the flashlight when your Application goes to background or when the Fonctionnalité is no longer needed to Enregistrer battery.

  4. Avoid rapid toggling Implement debouncing to prevent rapid on/off switching which could damage the LED or drain battery.

  • Requires iOS 10.0+
  • Uses AVCaptureDevice torch mode
  • Supports intensity control (0.0 to 1.0)
  • 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 Appareil’s flashlight
  • Requires Utilisateur 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