Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Using Windows phone 8.1 light sensor

From Wiki
Jump to: navigation, search
Featured Article
04 May
2014

This article explains how to use the new LightSensor API in Windows Phone 8.1

SignpostIcon HereMaps 99.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 8.1 SDK
Devices(s): Windows Phone devices with OS v8.1
Compatibility
Platform(s):
Windows Phone 8
Article
Created: andrejt (20 Apr 2014)
Last edited: kiran10182 (04 May 2014)

Contents

Introduction

You know those fancy car GPS navigators that automatically switch color mode to dark when it gets dark to make it easy on your eyes? The display even turns dark when you drive into a tunnel and lights back up when you’re out…

With an ambient-light sensor support in the latest Windows Phone 8.1 SDK, it’s possible to build something quite like that. The new API is quite straightforward and follows the same practice as do other device sensors.

Working with the light sensor

When your app starts, you should first query the device to see if it has a light sensor. The sensor's GetDefault() method will return null if the sensor isn't present on the device or the system was unable to get a reference to it. This might happen if the device is in a connected standby mode.

var sensor = Windows.Devices.Sensors.LightSensor.GetDefault();
if (sensor == null)
{
return;
}


Once you confirm access to the sensor, it's recommended that you set its reporting interval to the default value (0). In cases where you need to set it explicitly, you can do this by changing the sensor's ReportInterval property. Make sure you don't go below the MinimumReportInterval property. Here's the code for setting it to 5 minutes, but if you need more dynamic readings, just leave it at default:

sensor.ReportInterval = Math.Max(sensor.MinimumReportInterval, 5*60*1000);

Note.pngNote: Don't forget to set it back to the default value (0) when you're done.

To start reading, you can either perform a one-time read as follows:

var reading = sensor.GetCurrentReading();


… or subscribe to the ReadingChanged event for continuous reading:

sensor.ReadingChanged += OnLightSensorReadingChanged;

Note.pngNote: The frequency of the ReadingChanged event being raised is based on the previously set ReportInterval.

Either way, you'll get a LightSensorReading with information about the current reading. The illuminance level returned by this is in lux so it should be easy to compare with some known values. I figured 500 – 1000 would be a good value to detect a decent daylight and in this example I'm testing against a value of 1000 lux.

Using ambient light level in apps

There are various cases where light sensors might come in handy. Here are a few.

Making a theme respond to ambient light

A light sensor allows the user to have a theme that responds to light, e.g. have a light theme during the day and dark theme during the night. This might be used for reading apps, for example. Luckily, in Windows Phone 8.1, changing the theme for a page (or for any specific element) is now very simple. No more 3rd party helpers are needed. Here's an example of the OnLightSensorReadingChange event handler for the case, where the application theme will be adjusted according to the latest sensor reading:

void OnLightSensorReadingChanged(Windows.Devices.Sensors.LightSensor sender, Windows.Devices.Sensors.LightSensorReadingChangedEventArgs args)
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() => RequestedTheme = args.Reading.IlluminanceInLux < 1000
? ElementTheme.Dark
: ElementTheme.Light
);
}

Warning.pngWarning: Notice the relevant line is wrapped in a Dispatcher call – the LightSensorReadingChanged event will always be fired on a background thread so we have to get back to the UI thread to set a visual property properly.

Using ambient light level in maps

Any app that displays a map could use this feature, including navigation apps, locators, running apps, and more. The Map control has a very handy property to support this. Let's take a look at map's ColorScheme:

void OnLightSensorReadingChanged(Windows.Devices.Sensors.LightSensor sender, Windows.Devices.Sensors.LightSensorReadingChangedEventArgs args)
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() => Map.ColorScheme = args.Reading.IlluminanceInLux < 1000
? MapColorScheme.Dark
: MapColorScheme.Light;
);
}

Summary

Using the new LightSensor API in the Windows Phone 8.1 SDK takes only a few steps. Using it in your app can enhance your user's experience and take it that extra mile, while taking not that much effort from development side.

Tip.pngTip: The same LightSensor API is available to Windows Store apps as well.

Download source code: File:WPLightSensorSample.ZIP

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 4 May 2014, at 16:45.
467 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×