How do you read device sensors like the accelerometer?
Short answer
Use the sensor APIs (Accelerometer, Gyroscope, Compass, Barometer). Subscribe to ReadingChanged and call Start with a SensorSpeed, then Stop when done to save battery. Marshal UI updates to the main thread.
Advertisement
AModel answer
Use the sensor APIs (Accelerometer, Gyroscope, Compass, Barometer). Subscribe to ReadingChanged and call Start with a SensorSpeed, then Stop when done to save battery. Marshal UI updates to the main thread.
Accelerometer.Default.ReadingChanged += (s, e) =>
Debug.WriteLine(e.Reading.Acceleration);
Accelerometer.Default.Start(SensorSpeed.UI);Real-world example
A fitness app detects shakes and step motion by reading the accelerometer at UI speed and stopping it when the screen closes.
Likely follow-ups
- Why stop sensors when idle? | What is SensorSpeed?
Related topics