Switch the Flashlight / Torch of your device.
npm install @capgo/capacitor-flash
npx cap sync
Works out of the box
AndroidManifest.xml
file<!-- Permissions: Allows access to flashlight -->
<uses-permission android:name="android.permission.CAMERA" android:maxSdkVersion="23" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- Actual Hardware Features Used-->
<uses-feature android:name="android.hardware.camera.flash" android:required="true" />
isAvailable() => any
Checks if flashlight is available
Returns: any
switchOn(options: { intensity?: number; }) => any
Turns the flashlight on
Param | Type |
---|---|
options |
{ intensity?: number; } |
Returns: any
switchOff() => any
Turns the flashlight off
Returns: any
isSwitchedOn() => any
Checks if the flashlight is turned on or off
Returns: any
toggle() => any
Toggle the flashlight
Returns: any
The @capgo/capacitor-flash
package allows you to switch the flashlight/torch of your device on and off. In this tutorial, we will guide you through the process of installing and using this package in your Ionic Capacitor app.
To install the @capgo/capacitor-flash
package, run the following command in your project's root directory:
npm install @capgo/capacitor-flash
npx cap sync
The @capgo/capacitor-flash
package works out of the box on iOS, so no additional setup is required.
For Android, you need to declare the necessary permissions in your app's AndroidManifest.xml
file. Add the following lines inside the <manifest>
tag:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
The @capgo/capacitor-flash
package provides the following API methods:
This method checks if the flashlight is available on the device.
import { CapacitorFlash } from '@capgo/capacitor-flash';
async function checkFlashlightAvailability() {
const isAvailable = await CapacitorFlash.isAvailable();
console.log('Flashlight availability:', isAvailable);
}
This method turns on the flashlight of the device. You can pass options to adjust the intensity of the flashlight.
import { CapacitorFlash } from '@capgo/capacitor-flash';
async function switchOnFlashlight() {
const options = {
intensity: 100, // Set the intensity to 100%
};
await CapacitorFlash.switchOn(options);
console.log('Flashlight switched on');
}
This method turns off the flashlight of the device.
import { CapacitorFlash } from '@capgo/capacitor-flash';
async function switchOffFlashlight() {
await CapacitorFlash.switchOff();
console.log('Flashlight switched off');
}
This method checks if the flashlight is currently turned on or off.
import { CapacitorFlash } from '@capgo/capacitor-flash';
async function checkFlashlightStatus() {
const isSwitchedOn = await CapacitorFlash.isSwitchedOn();
console.log('Flashlight status:', isSwitchedOn ? 'ON' : 'OFF');
}
This method toggles the flashlight, i.e., if it is switched on, it will switch it off, and vice versa.
import { CapacitorFlash } from '@capgo/capacitor-flash';
async function toggleFlashlight() {
await CapacitorFlash.toggle();
console.log('Flashlight toggled');
}
That's it! You have successfully learned how to use the @capgo/capacitor-flash
package in your Ionic Capacitor app to control the flashlight/torch of your device.