Chapter 20. Using the Accelerometer

Windows Phone 7 contains a hardware-based sensor called the accelerometer. The accelerometer measures the phone's position relative to a line drawn from the phone to the center of the Earth, and is measured on three axes (x, y, z) in relation to this line.

The x-axis is the horizontal axis of the phone when the phone is held in the portrait orientation. The y-axis is the vertical axis when the phone is held in the portrait orientation. The z-axis is a line perpendicular to the surface of the phone. Figure 20-1 shows a representation of these three axes, as well as possible values for each axis.

Accessing the Accelerometer Class

Accessing the Accelerometer Class

The Accelerometer class is part of the Microsoft.Devices.Sensors namespace. To use the accelerometer you first need to add references in your project to the Microsoft.Devices.Sensors and Microsoft.Phone.Reactive assemblies. Then you will need to add a using reference to the following three namespaces:

using Microsoft.Devices;
using Microsoft.Devices.Sensors;
using Microsoft.Phone.Reactive;

Once you have these references in place you can create a new Accelerometer object:

private readonly Accelerometer accelerometer = new Accelerometer();

Determining the Current State of the Sensor

Before using the accelerometer you should check the state of the accelerometer sensor. You can do this using the Accelerometer.State property, which returns the current state of the accelerometer as an enumeration of type ...

Get Windows® Phone 7 Application Development 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.