,

Sensors Overview

All sensor classes derive from the class SensorBase<TSensorReading>, allowing you to work with what will become a familiar programming model. SensorBase ensures that sensors and readings are treated in the same way, a consistency that you will come to appreciate the more you work with sensors. The only variation occurs in the type of data that each sensor provides as a reading.

SensorBase<TSensorReading> has the following three properties:

Image CurrentValue

Image IsDataValid

Image TimeBetweenUpdates

Windows Phone sensors use an event-based model for monitoring changes to sensor readings. When new data arrives from the sensor, the SensorBase<TSensorReading>.CurrentValueChanged event is raised, providing the ability to periodically process sensor readings. The type of reading depends on the type of sensor you are using. For example, the accelerometer uses an AccelerometerReading.


Note

The CurrentValueChanged event is not raised on the UI thread. It is, therefore, important to invoke any updates to the UI using a Dispatcher. This, however, poses no challenge if you choose to use the property change notification infrastructure present in the downloadable sample code.


The interval between sensor readings can be set using the TimeBetweenUpdates property, which is of type TimeSpan. By default, this value is 17, 20, or 25 milliseconds, depending on the type of sensor. Setting the TimeBetweenUpdates to a value not supported by the sensor silently fails.


Note

If you require a specific value for the TimeBetweenUpdates interval, be sure to test the value of the property after it is set to ensure that the value was actually assigned.


The CurrentValue property holds the value of the most recent sensor reading taken. A good habit to get into is to make sure you check the IsDataValid property, which indicates whether the sensor’s CurrentValue property contains valid data before retrieving the value from the CurrentValue property.

Two methods, Start and Stop, are used to start and stop acquisition of data from the sensor. Sensors consume native resources on the device and may unduly impact battery consumption if they are kept alive for longer than they are needed. It is therefore important to call Start only when sensor readings are needed, and to call Stop as soon as the new sensor readings are no longer needed.

SensorBase<TSensorReading> implements IDisposable. Calling Dispose on a sensor releases the managed and unmanaged resources used by the sensor. Obviously it is wise to dispose of sensors when they are not needed, such as when your app is deactivated; however, SensorBase<T> automatically subscribes to the PhoneApplicationService.Closing event, and when the event is raised, calls its own Dispose method. This means that, no matter what happens, resources related to the native sensor are correctly released when your app exits.


Note

The accelerometer, compass, motion, and gyroscope are limited to 10 instances of each type. If this number is exceeded, a SensorFailedException is raised when the sensor class’s Start method is called. If this occurs, you can notify the user, providing this reason as a potential cause, and instruct the user to uninstall another app that uses the same sensor type.


The sensor classes available in the Windows Phone SDK reside in the Microsoft.Devices.Sensors namespace, and include the Accelerometer, Compass, Gyroscope, and Motion classes.

The following sections look at each sensor, beginning with the accelerometer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.240.61