Introduction
Modern mobile devices aren’t used just for calling people. They pack the same computing power as a last-gen computer. In addition, they have a lot of sensors to help with the various features of a “smartphone.” One of the available sensors is the ambient light sensor, which can detect ambient light. This sensor can be used to determine the LUX value of light.
Light Sensor Basics
Windows Phone has platform support for sensing ambient light in the form of the LightSensor that is housed in the Windows.Devices.Sensors namespace.
- The LightSensor class represents an ambient-light sensor that returns the light reading as a LUX value.
- The LightSensor class exposes the ReadingChanged event that is fired every time a new sensor reading is reported.
- The GetCurrentReading method on the LightSensor class is used to get the current LUX value.
We can learn about the minimum report internal by using the MinimumReportInterval property on the LightSensor class. We can set/get the report internal by using the ReportInterval property. A handle to the LightSensor object can be obtained by calling the LightSensor.GetDefault() method. If there is no light sensor on the device, the method will return null.
Hands On
In our hands on, we will build a simple application in which we will get the ambient light reading displayed in a textbox control.
Create a new Visual Studio 2013 Windows Phone Blank App project called WPLightSensorDemo.
Figure 1: The new blank project
Add a few controls as shown in the XAML layout depicted in Figure 2.
Figure 2: Placing the controls
<Page x_Class="WPLightSensorDemo.MainPage" xmlns_x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns_local="using:WPLightSensorDemo" xmlns_d="http://schemas.microsoft.com/expression/blend/2008" xmlns_mc="http://schemas.openxmlformats.org/ markup-compatibility/2006" mc_Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <TextBox x_Name="textBoxReading" HorizontalAlignment="Left" Margin="191,190,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="87"/> <TextBlock x_Name="textBlockReading" HorizontalAlignment="Left" Margin="134,202,0,0" TextWrapping="Wrap" Text="Reading" VerticalAlignment="Top" RenderTransformOrigin="1.73,2.074"/> <TextBox x_Name="textBoxReportingInterval" HorizontalAlignment="Left" Margin="191,58,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="87"/> <TextBlock x_Name="textBlockReportingInterval" HorizontalAlignment="Left" Margin="99,72,0,0" TextWrapping="Wrap" Text="Reporting Interval" VerticalAlignment="Top"/> <TextBox x_Name="textBoxReadingTime" HorizontalAlignment="Left" Margin="191,233,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="152"/> <TextBlock HorizontalAlignment="Left" Margin="77,244,0,0" TextWrapping="Wrap" Text="Reading Timestamp" VerticalAlignment="Top"/> </Grid> </Page>
Next, we make changes in the code-behind for MainPage.xaml. First, we include a couple of namespaces in the source file.
using Windows.Devices.Sensors;
using Windows.UI.Core;
Next, we create a couple of class variables: one for the LightSensor and another to track whether we detected a light sensor.
public sealed partial class MainPage : Page { LightSensor ls; bool sensorFound = false; public MainPage()
Next, we update the constructor to instantiate the lightsensor variable and wire up its ReadingChanged event.
public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; ls = LightSensor.GetDefault(); if (ls != null) { sensorFound = true; ls.ReadingChanged += ls_ReadingChanged; textBoxReportingInterval.Text = ls.MinimumReportInterval.ToString(); ls.ReportInterval = ls.MinimumReportInterval; } }
Finally, we implement the event handler for the ReadingChanged event.
async void ls_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { textBoxReading.Text = args.Reading.IlluminanceInLux.ToString(); textBoxReadingTime.Text = args.Reading.Timestamp.ToString(); }); }
Our application is now ready. When we run the application the first time, we will set the recording interval to the lowest possible and update the textbox to reflect the ambient light reading as well as the timestamp of the reading. If you are having trouble following along, you can download the sample code from the bottom of this article.
Summary
In this article, we learned about using a light sensor in Windows Phone applications.
About the Author
Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.