Windows 8 Consumer Preview and Visual Studio 11 Beta – Quick tip: using sensors (Part 6/11)

February 29, 2012

Windows 8 supports multiple sensors. Some of them could be installed on your device, some of them not.

When I planned this series, I had pretty hard choice to make about the sensor – which one to use. Accelerometer/Gyrometer/Compass/Inclinometer/Orientation are cool sensors, but their usage pretty tightly bound to specific scenarios like games (Accelerometer/Gyrometer/Inclinometer), some location aware applications (Compass) or at least orientation-aware apps (Orientation). I had to make a choice either create simple “show-the-values” boring sample or game which using those sensors (at least some of them) or showcase go with non-standard approach presenting LightSensor which was easy bound to my sample app.

Since all the sensors supports same APIs, I decided to go with latter approach.

Working with all sensors are roughly the same as scenario presented in this simple post. To initialize sensor, use GetDefault function:

//Initialize the sensor
lightSensor = LightSensor.GetDefault();

if (null != lightSensor)
{
    //Sensor is supported!
    //Optionally set reporting interval. Cannot exceed hardware supported minimum reporting inteval
    lightSensor.ReportInterval = lightSensor.MinimumReportInterval;
    //Subscribe to ReadingChanged event to handle new value
    lightSensor.ReadingChanged += new TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>(LightSensor_ReadingChanged);
}
else
{
    //Sensor is not supported. Use alternative app path 
    //...
}

If specific sensor supported you can optionally update ReportInterval property specifying how fast/slow your app requires new data. Polling new data slowly helps to preserve battery life (on battery powered devices) but can possible degrade the application functionality (especially in game scenarios when sensor, such as accelerometer, is used for main input).

Note: ReportInterval cannot be lower than hardware reported MinimumReportInterval provided by sensor.

Working with sensor possible using two approaches – polling current reading when necessary using GetCurrentReading function:

var currentReading = lightSensor.GetCurrentReading();

or provide event handler function which invoked every time sensor has a new value (like showed in first code snippet). In both cases, sensor reading object provides all relevant information (for sensor) + reading timestamp. In my sample app LightSensorReading used to change application’s background color:

private void LightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{
    Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
    {
        LightSensorReading reading = (a.Context as LightSensorReadingChangedEventArgs).Reading;
        LayotRoot.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, (byte)(reading.IlluminanceInLux), 0, 0));
    }, this, args);
}

Note: The event handler arrives out of UI thread, so Dispatcher.InvokeAsync technique must be used to access UI elements.

 

That’s in for now. Stay tuned for next post.

Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*