Every Windows Phone 7 device contains an accelerometer, which is basically a motion-detecting sensor. It measures acceleration in three dimensions and makes this information available to applications. This acceleration includes gravity, so the phone knows which way is up (used under the hood for orientation detection) as well as other sources, such as those imparted by a user.

There are a number of uses for accelerometer data, from user interface streamlining to gaming. Games developers in particular have been quick on the uptake, and already there are, for example, driving games that let you steer by tilting a device, and innumerable marble maze–type games.

In this section, you'll see how to access accelerometer data and see some examples of using that data in applications. You'll also look at how to simulate sensor data, which is essential when working in the emulator.

The AccelerometerSensor Sensor

The Windows Phone 7 SDK includes sensor types in the Microsoft.Devices.Sensors namespace, for which you need the Microsoft.Devices.Sensors.dll assembly referenced by your project. At the time of writing, there are four types in this namespace, as shown in Table 6-2.

TABLE 6-2: Sensor Namespace Types


The SensorState enumeration defines a standard set of values for states appropriate to sensors. Table 6-3 shows these values.

TABLE 6-3: SensorState ...

Get Beginning Windows® Phone 7 Application Development: Building Windows® Phone Applications Using Silverlight® and XNA® now with O’Reilly online learning.

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