Using the LightSensor Class in Your Windows Store Apps

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.

LightSensor1

Figure 1: The new blank project

Add a few controls as shown in the XAML layout depicted in Figure 2.

LightSensor2
Figure 2: Placing the controls

<Page
   x:Class="WPLightSensorDemo.MainPage"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
      presentation"
   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.

References



Related Articles

Downloads

Comments

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

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

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date