Skip to main content

@napacitor/flashlight

Flashlight control plugin wrapping the .NET MAUI Flashlight API.

Installation​

To use npm:

npm install @napacitor/flashlight

To use yarn:

yarn add @napacitor/flashlight

Sync native files:

npx nap sync

Usage​

import { Flashlight } from '@napacitor/flashlight';

// Check for support
const { supported } = await Flashlight.isSupported();

if (supported) {
// Turn the flashlight on
await Flashlight.switchOn({ intensity: 0.8 });

// Query state
const { isOn } = await Flashlight.getState();

// Toggle state
await Flashlight.toggle();

// Turn the flashlight off
await Flashlight.switchOff();
}

How It Works​

Wraps the .NET MAUI Flashlight.Default.TurnOnAsync / TurnOffAsync / IsSupportedAsync facade (Android uses Torch Mode / iOS uses AVCaptureDevice / Windows uses the Lamp API). The _isOn state is maintained by the plugin itself (MAUI has no state query), and toggle() switches based on that state. Platforms without flashlight support (such as Windows desktop) throw UNSUPPORTED. Note: intensity is only partially supported; the native side currently ignores it (the MAUI API has no brightness parameter).

Permissions​

Android​

The FLASHLIGHT and CAMERA permissions must be declared (required by the MAUI documentation). Add them to AndroidManifest.xml:

<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />

Or add assembly attributes in Platforms/Android/MainApplication.cs:

[assembly: UsesPermission(Android.Manifest.Permission.Flashlight)]
[assembly: UsesPermission(Android.Manifest.Permission.Camera)]

To avoid Google Play automatically filtering out devices without a camera, declare the camera hardware feature as optional:

[assembly: UsesFeature("android.hardware.camera", Required = false)]

iOS​

No additional configuration required.

Windows​

No additional configuration required.

API​

isSupported()​

Checks whether the current device supports the flashlight.

Returns: Promise<FlashlightSupportedResult>

switchOn(options?)​

Turns the flashlight on (throws UNSUPPORTED when the device does not support it).

ParamType
optionsFlashlightOptions

Returns: Promise<void>

switchOff()​

Turns the flashlight off (throws UNSUPPORTED when the device does not support it).

Returns: Promise<void>

toggle(options?)​

Toggles the flashlight state (on → off / off → on).

ParamType
optionsFlashlightOptions

Returns: Promise<void>

getState()​

Queries the current flashlight state (the _isOn state maintained by the plugin, not a system query).

Returns: Promise<FlashlightStateResult>

Interfaces​

FlashlightOptions​

PropTypeDescription
intensitynumberBrightness level (0.0 - 1.0); only partially supported, default 1.0. The native side currently ignores it (the MAUI API has no brightness parameter).

FlashlightStateResult​

PropTypeDescription
isOnbooleanWhether the flashlight is currently on.

FlashlightSupportedResult​

PropTypeDescription
supportedbooleanWhether the current device supports the flashlight.