How to get Accelerometer support in Windows Phone 8 Application

Prerequisites
Before reading this article, download and install Windows Phone 8 SDK, if you don't have it.
Defining Accelerometer in Mobile devices
An accelerometer is a sensor which measures the tilting motion and orientation of a mobile device.
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 Accelerometer Sensor.  


The Accelerometer sensor can be used to get a acceleration force of the device on the x y and z axis, or basically measure how fast it is moving in any given direction.  





The above diagram shows Axis of Accelerometer in Windows Phone 8 device. Clearly, Windows Phone 8 application depicts the Roll of smartphone as X, pitch as Z and Yaw as Y.


With the Accelerometer you could build interesting augmented reality applications which react to the forces of the device.  In this post we are going to take a look at how to use the Accelerometer Sensor from within your C#/XAML Windows 8 Phone application.

In this code, we will "get support" of Accelerometer from the smartphone. i.e to Check if the device has an accelerometer sensor.



Fetch Accelerometer support in Windows Phone 8





private void getsupport()
{
    switch (Microsoft.Devices.Sensors.Accelerometer.IsSupported)
            {
                case true:
                    {
                        accel.TimeBetweenUpdates = TimeSpan.FromMilliseconds(16);
                        accel.ReadingChanged += accel_ReadingChanged;
                        break;
                    }
                case false:
                    {
                        MessageBox.Show("Your device does not have Accelerometer Sensors. This application requires device with working accelerometer.", "No Acceleromter detected”,  MessageBoxButton.OK);
                        break;
                    }
            }                     




In the above code what we are doing is making the call to get the sensor support, this is the sensor on the device.  If there is NO sensor on the users device, then Message Box is shown up saying Device does not support this application.  Make sure you do the same in your code.
In next article, we will study how to get Accelerometer values and create creative and amazing Accelerometer based Windows Phone 8 Application.
Until next,

1 Comments

Previous Post Next Post