Using the LightSensor Class in Your Windows Store Apps


Application Security Testing: An Integral Part of DevOps


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


      <TextBox x:Name="textBoxReading"
         HorizontalAlignment="Left" Margin="191,190,0,0"
         TextWrapping="Wrap" VerticalAlignment="Top"
      <TextBlock x:Name="textBlockReading"
         HorizontalAlignment="Left" Margin="134,202,0,0"
         TextWrapping="Wrap" Text="Reading"
      <TextBox x:Name="textBoxReportingInterval"
         HorizontalAlignment="Left" Margin="191,58,0,0"
         TextWrapping="Wrap" VerticalAlignment="Top"
      <TextBlock x:Name="textBlockReportingInterval"
         HorizontalAlignment="Left" Margin="99,72,0,0"
         TextWrapping="Wrap" Text="Reporting Interval"
      <TextBox x:Name="textBoxReadingTime"
         Margin="191,233,0,0" TextWrapping="Wrap"
         VerticalAlignment="Top" Width="152"/>
      <TextBlock HorizontalAlignment="Left"
         TextWrapping="Wrap" Text="Reading Timestamp"

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.NavigationCacheMode = NavigationCacheMode.Required;
      ls = LightSensor.GetDefault();
      if (ls != null)
         sensorFound = true;
         ls.ReadingChanged += ls_ReadingChanged;
         textBoxReportingInterval.Text =
         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 =
         textBoxReadingTime.Text =


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.


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.


Related Articles



  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.