.NET MAUI › Device Features & Essentials

How do you read device sensors like the accelerometer?

Intermediate

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.

Common follow-ups: Why stop sensors when idle? | What is SensorSpeed?