Click here to Skip to main content
15,884,629 members
Articles / Desktop Programming / XAML
Tip/Trick

Accelerometer Sensor in Windows Phone 8

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
20 Feb 2013CPOL2 min read 22K   764   5   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. XML
    //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

    C#
    // Instantiate the Accelerometer.
    accelerometer = new Accelerometer();
    accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(500);
    accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
    accelerometer.Start();

    CurrentValueChanged Callback Handler

    C#
    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.
    C#
    accelerometer.Stop();

You can download the attached source code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Michael Haephrati20-Feb-13 6:38
professionalMichael Haephrati20-Feb-13 6:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.