Derik Whittaker

Syndication

News


How to use the Light Sensor in Windows 8 C#/XAML applications

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

image

Now on to how to use the Light Sensor

In the code above I have done 2 things:

  1. Created a instance property for the LightSensor
  2. 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.

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

Doing something useful w/ the Lux values

Two things should stand out from the above code.

  1. I am using the async/await keywords, this just helps w/ the UI responsiveness
  2. 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
Filed under: , , ,

[Advertisement]

Comments

Derik Whittaker wrote How to use the Gyrometer Sensor in Windows 8 C#/XAML applications
on 01-21-2013 6:55 PM

This post is part of a mulit-part posting series on how to use some of the onboard device sensors in

Derik Whittaker wrote How to use the Inclinometer sensor in Windows 8 C#/XAML applications
on 01-23-2013 9:30 AM

This post is part of a mulit-part posting series on how to use some of the onboard device sensors in

Derik Whittaker wrote How to use the Accelerometer in Windows 8 C#/XAML Applications
on 01-27-2013 3:15 PM

This post is part of a mulit-part posting series on how to use some of the onboard device sensors in

Alex wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 01-27-2013 8:43 PM

Did you try running your code on surface?

With 1 lightbulb on my kitchen it shows lux in range from 2 to 7-8 (rarely).

Derik Whittaker wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 01-27-2013 8:46 PM

@Alex,

No, I ran it on my Samsung Slate and would get readings around 75ish with a pretty bright overhead light.

I will try to run it remotely on my Asus Vivotab this week.

buy viagra online wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 02-03-2013 5:09 AM

ca5PRx Very neat blog.Thanks Again. Awesome.

buy viagra wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 02-03-2013 2:29 PM

F9KbRc I truly appreciate this blog post.Much thanks again. Fantastic.

bookmaring service wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 03-15-2013 7:13 AM

8PMraI Say, you got a nice blog.Thanks Again. Great.

Social bookmarks wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 03-24-2013 6:51 PM

4rIzeO I cannot thank you enough for the blog post. Really Great.

buy social bookmarks wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 03-25-2013 3:42 AM

X32n2N Thanks so much for the post.Really looking forward to read more. Much obliged.

nonsense wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 04-06-2013 10:15 AM

Really enjoyed this post. Keep writing.

Social bookmarks wrote re: How to use the Light Sensor in Windows 8 C#/XAML applications
on 04-13-2013 11:26 AM

RUX7Lz Really enjoyed this article post.Thanks Again. Keep writing.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)