Camera-independent Flashlight
This article shows how to access and use a device's lamp, if one is present. Lamp functionality is managed separately from the device's camera and camera flash functionality. In addition to acquiring a reference to the lamp and adjusting its settings, this article also shows you how to properly free up the lamp resource when it's not in use, and how to detect when the lamp's availability changes in case it is being used by another app.
Get the device's default lamp
To get a device's default lamp device, call Lamp.GetDefaultAsync. The lamp APIs are found in the Windows.Devices.Lights namespace. Be sure to add a using directive for this namespace before attempting to access these APIs.
using Windows.Devices.Lights;
Lamp lamp;
lamp = await Lamp.GetDefaultAsync();
if (lamp == null)
{
ShowErrorMessage("No Lamp device found");
return;
}
If the returned object is null, the Lamp API is unsupported on the device. Some devices may not support the Lamp API even if there is a lamp physically present on the device.
Get a specific lamp using the lamp selector string
Some devices may have more than one lamp. To obtain a list of lamps available on the device, get the device selector string by calling GetDeviceSelector. This selector string can then be passed into DeviceInformation.FindAllAsync. This method is used to enumerate many different kinds of devices and the selector string lets the method know to return only lamp devices. The DeviceInformationCollection object returned from FindAllAsync is a collection of DeviceInformation objects representing the lamps available on the device. Select one of the objects in the list and then pass the Id property to Lamp.FromIdAsync to get a reference to the requested lamp. This example uses the GetFirstOrDefault extension method from the System.Linq namespace to select the DeviceInformation object where the EnclosureLocation.Panel property has a value of Back, which selects a lamp that is on the back of the device's enclosure, if one exists.
Note that the DeviceInformation APIs are found in the Windows.Devices.Enumeration namespace.
using Windows.Devices.Enumeration;
using System.Linq;
string selectorString = Lamp.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selectorString);
DeviceInformation deviceInfo =
devices.FirstOrDefault(di => di.EnclosureLocation != null &&
di.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
if (deviceInfo == null)
{
ShowErrorMessage("No Lamp device found");
}
lamp = await Lamp.FromIdAsync(deviceInfo.Id);
Adjust lamp settings
After you have an instance of the Lamp class, turn the lamp on by setting the IsEnabled property to true.
lamp.IsEnabled = true;
Turn the lamp off by setting the IsEnabled property to false.
lamp.IsEnabled = false;
Some devices have lamps that support color values. Check if a lamp supports color by checking the IsColorSettable property. If this value is true, you can set the color of the lamp with the Color property.
if (lamp.IsColorSettable)
{
lamp.Color = Windows.UI.Colors.Blue;
}
Register to be notified if the lamp availability changes
Lamp access is granted to the most recent app to request access. So, if another app is launched and requests a lamp resource that your app is currently using, your app will no longer be able to control the lamp until the other app has released the resource. To receive a notification when the availability of the lamp changes, register a handler for the Lamp.AvailabilityChanged event.
lamp = await Lamp.GetDefaultAsync();
if (lamp == null)
{
ShowErrorMessage("No Lamp device found");
return;
}
lamp.AvailabilityChanged += Lamp_AvailabilityChanged;
In the handler for the event, check the LampAvailabilityChanged.IsAvailable property to determine if the lamp is available. In this example, a toggle switch for turning the lamp on and off is enabled or disabled based on the lamp availability.
private void Lamp_AvailabilityChanged(Lamp sender, LampAvailabilityChangedEventArgs args)
{
lampToggleSwitch.IsEnabled = args.IsAvailable;
}
Properly dispose of the lamp resource when not in use
When you are no longer using the lamp, you should disable it and call Lamp.Close to release the resource and allow other apps to access the lamp. This property is mapped to the Dispose method if you are using C#. If you registered for the AvailabilityChanged, you should unregister the handler when you dispose of the lamp resource. The right place in your code to dispose of the lamp resource depends on your app. To scope lamp access to a single page, release the resource in the OnNavigatingFrom event.
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
lamp.AvailabilityChanged -= Lamp_AvailabilityChanged;
lamp.IsEnabled = false;
lamp.Dispose();
lamp = null;
}
Related topics