Most tablets and laptops these days have an array of sensors onboard the device that developers can use and take advantage of. One of the sensors which is on most tables and laptops is the Light Sensor. This sensor can be used for many things and it is common that the light sensor is used to illuminate the backlight on keyboards. In this post we are going to take a look at how to use the Light Sensor from within your C#/XAML Windows 8 application.
Before we get started we should understand what the light sensor reading is providing you. This sensor will give you the ‘Lux Values’ (which is lumens per square meter) from the ambient light in your area. The Lux values can be described in the chart below and you can get more details by looking at the MSDN link here
Now on to how to use the Light Sensor
private Windows.Devices.Sensors.LightSensor _lightSensor;
private void SetupLightSensor()
{
_lightSensor = Sensor.LightSensor.GetDefault();
if (_lightSensor == null)
{
LuxLums = "Your current device does not suppor the light sensor";
}
}
In the code above I have done 2 things:
- Created a instance property for the LightSensor
- Retrieved the current light sensor and done a check to see if it is null. It being null will indicate that your device does not have a sensor
Register to receive updates from the sensor as the values change
Now that we have our light sensor we want to register for its updates. To do this we can either ask the sensor for its reading one time, we can poll the sensor or we can register for an ‘ReadingChanged’ event. I am going to do the register for updates via the event.
private void SetupEventing(bool enableEventing)
{
if (enableEventing)
{
_lightSensor.ReadingChanged += LightSensorOnReadingChanged;
_lightSensor.ReportInterval = 100;
CurrentReadingStyle = "Eventing";
}
else
{
_lightSensor.ReadingChanged -= LightSensorOnReadingChanged;
CurrentReadingStyle = "Stopped";
}
}
The above code will simply register/unregister the event handler based on the provided flat.
Getting the reading once or via Polling
If you would like to simply get the Lux value one time you could just do the following
var lightSensorReading = _lightSensor.GetCurrentReading();
Doing something useful w/ the Lux values
private async void LightSensorOnReadingChanged(Sensor.LightSensor sender, Sensor.LightSensorReadingChangedEventArgs args)
{
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
var lightSensorReading = args.Reading;
LuxLums = string.Format("{0,5:0.00}", lightSensorReading.IlluminanceInLux);
});
}
Two things should stand out from the above code.
- I am using the async/await keywords, this just helps w/ the UI responsiveness
- I am using a dispatcher to push the UI changes. I get access to the dispatcher by passing it into my ViewModel via the constructor and I am passing in an instance of Window.Current.Dispatcher
Now once I have access to the reading above I am simply pushing this value to the UI for display but it is here that you may want to do something useful
As you can see working with the light sensor is not too hard and can lead to some pretty useful features in your application.
Till next time,
Posted
01-16-2013 1:04 PM
by
Derik Whittaker