Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Accelerometer Sensor in Windows Phone 8

0.00/5 (No votes)
20 Feb 2013 1  
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 

  1. Open VS2012 and select Windows Phone Category then create a new Windows Phone project .
  2. 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. 
  3. 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:  
  4. //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>
  5. 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 in CurrentValueChanged callback. In this example accelerometer gives us the update for every  5 seconds which is assigned to TimeBetweenUpdates property.
  6. 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
        }
    }
  7. Click on Stop button to stop the updates from accelerometer. Call the Stop method of the Accelerometer.
    accelerometer.Stop();

You can download the attached source code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here