Démarrage
-
Installer le package
Fenêtre de terminal npm i @capgo/capacitor-flashFenêtre de terminal pnpm add @capgo/capacitor-flashFenêtre de terminal yarn add @capgo/capacitor-flashFenêtre de terminal bun add @capgo/capacitor-flash -
Synchroniser avec les projets natifs
Fenêtre de terminal npx cap syncFenêtre de terminal pnpm cap syncFenêtre de terminal yarn cap syncFenêtre de terminal bunx cap sync
Utilisation
Section titled “Utilisation”Importez le plugin et utilisez ses méthodes pour contrôler la lampe torche :
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();};Référence API
Section titled “Référence API”isAvailable()
Section titled “isAvailable()”Vérifie si la lampe torche est disponible sur l’appareil.
const result = await CapacitorFlash.isAvailable();// Returns: { value: boolean }switchOn(options?)
Section titled “switchOn(options?)”Allume la lampe torche.
interface SwitchOnOptions { intensity?: number; // 0.0 to 1.0 (iOS only)}
await CapacitorFlash.switchOn({ intensity: 0.5 });switchOff()
Section titled “switchOff()”Éteint la lampe torche.
await CapacitorFlash.switchOff();isSwitchedOn()
Section titled “isSwitchedOn()”Vérifie si la lampe torche est actuellement allumée.
const result = await CapacitorFlash.isSwitchedOn();// Returns: { value: boolean }toggle()
Section titled “toggle()”Bascule la lampe torche (on/off).
await CapacitorFlash.toggle();Exemple complet
Section titled “Exemple complet”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; }}Bonnes pratiques
Section titled “Bonnes pratiques”-
Vérifiez la disponibilité en premier Vérifiez toujours la disponibilité de la lampe torche avant de l’utiliser :
const { value } = await CapacitorFlash.isAvailable();if (!value) {// Show alternative UI or disable flashlight features} -
Gérez les erreurs gracieusement
try {await CapacitorFlash.switchOn();} catch (error) {console.error('Failed to turn on flashlight:', error);} -
Éteignez quand ce n’est pas nécessaire Éteignez toujours la lampe torche lorsque votre application passe en arrière-plan ou lorsque la fonctionnalité n’est plus nécessaire pour économiser la batterie.
-
Évitez les basculements rapides Implémentez un anti-rebond pour éviter les allumages/extinctions rapides qui pourraient endommager la LED ou vider la batterie.
Notes de plateforme
Section titled “Notes de plateforme”- Nécessite iOS 10.0+
- Utilise le mode torche
AVCaptureDevice - Prend en charge le contrôle de l’intensité (0.0 à 1.0)
Android
Section titled “Android”- Nécessite Android 6.0 (API 23)+
- Utilise l’API Camera2
- Le contrôle de l’intensité n’est pas pris en charge (utilise la luminosité par défaut)
- Pris en charge sur les navigateurs avec API MediaDevices et capacité torche
- Utilise le flux de la caméra pour accéder à la lampe torche de l’appareil
- Nécessite la permission de l’utilisateur pour accéder à la caméra
- Fonctionne mieux sur les navigateurs mobiles (Chrome sur Android)
- Les navigateurs de bureau renvoient généralement
isAvailable: false(pas de matériel torche) - Le contrôle de l’intensité n’est pas pris en charge sur le web