.NET MAUI › Device Features & Essentials
How do you read device sensors like the accelerometer?
IntermediateAnswer
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.