Accelerometer Sensor in Windows Phone 8





5.00/5 (2 votes)
Implementation of Accelerometer in WP8 applications.
Introduction
From this article, you will get a basic idea of developing the sensor based applications using accelerometer in Windows Phone 8.
Background
Windows Phone 8 supports multiple sensors to determine the motion and orientations of the device like Accelerometer, Compass Sensor, Gyroscope and combined motions. With the use of these sensors in your applications, the physical device itself acts as user input.
Accelerometer allows applications to capture the acceleration along the X, Y, and Z axes in real time. So an accelerometer can report the direction and magnitude of the force being applied to it. The acceleration value is expressed as a 3-dimensional vector representing the acceleration components in the X - Horizontal, Y - Vertical, and Z - Perpendicular to the base. Every dimension value can range from '-2' to '2' in gravitational units.
Using the code
Let's see the implementation of this
- Open VS2012 and select Windows Phone Category then create a new Windows Phone project .
- To make use of sensors, add Microsoft.Devices.Sensors and
Microsoft.Xna.Framework references to your project. Also make sure that
ID_CAP_SENSORS
capability is checked in capabilities of App manifest file which is under Properties folder of your project. - Now this is my User Interface, where I have two buttons to start and stop the capturing magnitude and i have a Pivot control. So on tilting the Phone towards Right side with certain magnitude, the index of pivot element should be changed and should navigate to the next item. Below is the XAML:
- So good to go with UI, now we want to start capturing the magnitude of force being applied on click of Start button.
Accelerometer is the API which gives the value of x, y, z axis as
SensorReading
inCurrentValueChanged
callback. In this example accelerometer gives us the update for every 5 seconds which is assigned toTimeBetweenUpdates
property. Click
on Stop button to stop the updates from accelerometer. Call theStop
method of the Accelerometer.accelerometer.Stop();
//XAML
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="Simple Sensor Application" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<StackPanel Orientation="Horizontal">
<Button Content="Start Sensor" Click="Button_Click_1"
Height="80" Width="200" HorizontalAlignment="Left"/>
<Button Content="Stop Sensor" Click="Button_Click_2"
Height="80" Width="200" HorizontalAlignment="Left"/>
</StackPanel>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:Pivot Name="pivot">
<phone:PivotItem Header="Item1"> <TextBlock>AAAA</TextBlock> </phone:PivotItem>
<phone:PivotItem Header="Item2"> <TextBlock>BBBB</TextBlock> </phone:PivotItem>
<phone:PivotItem Header="Item3"> <TextBlock>CCCC</TextBlock> </phone:PivotItem>
<phone:PivotItem Header="Item4"> <TextBlock>DDDD</TextBlock> </phone:PivotItem>
<phone:PivotItem Header="Item5"> <TextBlock>EEEE</TextBlock> </phone:PivotItem>
<phone:PivotItem Header="Item6"> <TextBlock>FFFF</TextBlock> </phone:PivotItem>
</phone:Pivot>
</Grid>
On Start Button Click
// Instantiate the Accelerometer.
accelerometer = new Accelerometer();
accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(500);
accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
accelerometer.Start();
CurrentValueChanged
Callback Handler
void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
Vector3 acceleration = e.SensorReading.Acceleration;
// If user tilts the phone with 0.35 in X axis, Handle/Change the pivot index
if (acceleration.X <= -0.35)
{
// Application Logic
// Change the pivot control index by -1
}
if (acceleration.X >= 0.35)
{
// Application Logic
// change the pivot control index by +1
}
}
You can download the attached source code.