Flashlight
This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IFlashlight interface. With this interface, you can toggle the device's camera flash on and off, to emulate a flashlight.
The default implementation of the IFlashlight
interface is available through the Flashlight.Default property. Both the IFlashlight
interface and Flashlight
class are contained in the Microsoft.Maui.Devices
namespace.
Get started
To access the flashlight functionality the following platform-specific setup is required.
There are two permissions to configure in your project: Flashlight
and Camera
. These permissions can be set in the following ways:
Add the assembly-based permission:
Open the Platforms/Android/MainApplication.cs file and add the following assembly attributes after
using
directives:[assembly: UsesPermission(Android.Manifest.Permission.Flashlight)] [assembly: UsesPermission(Android.Manifest.Permission.Camera)]
- or -
Update the Android Manifest:
Open the Platforms/Android/AndroidManifest.xml file and add the following in the
manifest
node:<uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.CAMERA" />
- or -
Update the Android Manifest in the manifest editor:
In Visual Studio double-click on the Platforms/Android/AndroidManifest.xml file to open the Android manifest editor. Then, under Required permissions check the FLASHLIGHT and CAMERA permissions. This will automatically update the AndroidManifest.xml file.
If you set these permissions, Google Play will automatically filter out devices without specific hardware. You can get around this filtering by adding the following assembly attributes to the Platforms/Android/MainApplication.cs file after using
directives:
[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
Use Flashlight
The flashlight can be turned on and off through the TurnOnAsync() and TurnOffAsync() methods. The following code example ties the flashlight's on or off state to a Switch control:
private async void FlashlightSwitch_Toggled(object sender, ToggledEventArgs e)
{
try
{
if (FlashlightSwitch.IsToggled)
await Flashlight.Default.TurnOnAsync();
else
await Flashlight.Default.TurnOffAsync();
}
catch (FeatureNotSupportedException ex)
{
// Handle not supported on device exception
}
catch (PermissionException ex)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to turn on/off flashlight
}
}
In addition, the IsSupportedAsync method can be invoked to check if a flashlight is available on the device, prior to calling the TurnOnAsync() method.
Platform differences
This section describes the platform-specific differences with the flashlight.
The Flashlight
class has been optimized based on the device's operating system.
API level 23 and higher
On newer API levels, Torch Mode will be used to turn on or off the flash unit of the device.
API level 22 and lower
A camera surface texture is created to turn on or off the FlashMode
of the camera unit.