Sensors for Windows Phone 8
May 10, 2014
Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1
Windows Phone supports multiple sensors that allow apps to determine the orientation and motion of the device. These sensors enable the development of apps where the physical device itself is a means of user input. Typical uses for this functionality include motion-controlled games and augmented reality apps. For many of these purposes, the combined motion API, which combines and processes input from all sensors, is the simplest way to obtain motion and orientation information. For apps that require it, Windows Phone also provides APIs to retrieve data from the individual sensors. This topic provides a brief overview of each sensor API.
Each of the sensor APIs is built on the base class SensorBase<TSensorReading> and follow a similar pattern of starting the sensor and acquiring data. For each sensor, there is a how-to topic that quickly walks you through using sensors in your app.
In addition to the APIs described in this topic from the Microsoft.Devices.Sensors namespace, you can also program the phone’s sensors by using the similar classes in the Windows.Devices.Sensors namespace.
The accelerometer measures the forces applied to the device at a moment in time. These forces can be used to determine in which direction the user is moving the device. The acceleration value is expressed as a 3-dimensional vector representing the acceleration components in the X, Y, and Z axes in gravitational units. The orientation of the acceleration is relative to the device such that -1g is applied in the Z-axis when the device is face up on a level table and -1g is applied to the Y-axis when the device is placed perpendicular to the table top.
The Accelerometer sensor detects the force of gravity along with any forces resulting from the movement of the phone. The combined motion API, accessed using the Motion class, uses multiple device sensors to separate the gravity vector from the device acceleration and allows you to easily determine the current attitude (yaw, pitch, and roll) of the device.
The Compass, or magnetometer, sensor can be used to determine the angle by which the device is rotated relative to the Earth’s magnetic north pole. An app can also use raw magnetometer readings to detect magnetic forces around the device.
The compass sensor is not required for all Windows Phone devices. It is important that you consider this when designing and implementing your app. Your app should always check to see whether the sensor is available and either provide an alternative input mechanism or fail gracefully if it is not.
The Gyroscope sensor is used to determine the rotational velocity of the device in each axis. You can use the values obtained from the gyroscope sensor to determine the orientation of the device in space. Because a gyroscope measures rotational velocity and not angle, it is susceptible to issues related to drift. If you are interested in determining the attitude (yaw, pitch, and roll) of the device, you should use Motion instead of the gyroscope API.
The gyroscope sensor is not required for all Windows Phone devices. It is important that you consider this when designing and implementing your app. Your app should always check to see whether the sensor is available and either provide an alternative input mechanism or fail gracefully if it is not.
There are physical limitations that can make it difficult to determine the actual orientation and motion of a device from the raw data from the sensors by Windows Phone. For example, readings from the accelerometer include the force of gravity applied to the device in addition to the force resulting from the motion of the device. The gyroscope sensor measures rotational velocity, not position, and so it is subject to drift. Also, there are complex geometrical calculations that are needed to translate the raw data from these sensors into the true orientation of the device. The Motion class handles the low-level sensor calculation and allows apps to easily obtain the device’s attitude (yaw, pitch, and roll), rotational acceleration, and linear acceleration both due to gravity and user movement. For typical apps that use this type of data, such as augmented reality apps, using the Motion class is recommended. If you want to use sensor data in an atypical manner or want to implement your own motion and orientation calculations, you can use the individual sensor classes to obtain raw data from each sensor.
The Motion API can use two different sensor configurations. Normal Motion uses the compass and the accelerometer sensor and is less accurate than Enhanced Motion, which uses the compass, the accelerometer, and the gyroscope. If your app requires the accuracy of Enhanced motion, you should check to verify that the device on which the app is running supports the gyroscope sensor.
When a user views an app in the Store, a warning is issued if the user’s device does not support a sensor that is used by the app. For the accelerometer, compass, and gyroscope this is straightforward – if an app uses the sensor and the user’s device does not support it, the user will receive a warning.
As stated above, the Motion API will function on a device that has at least a compass sensor. It will, however, yield better data if a gyroscope is present. Currently, the Store uses the compass sensor to determine if the Motion API is supported on a device. If a user views an app that uses the Motion API and their device lacks a compass, they will receive a warning that the app requires a compass sensor.