Accelerometer

Provides access to the device's accelerometer sensor, which detects acceleration forces along three axes (X, Y, and Z).

Namespace

Windows.Devices.Sensors

Class

Accelerometer

Introduction

The accelerometer sensor is a fundamental input device for many modern applications, enabling features like screen rotation, motion-based gaming, and gesture recognition. UWP provides a straightforward API to access this sensor data.

Getting an Instance

To use the accelerometer, you first need to get an instance of the Accelerometer class. You can do this asynchronously using GetDefaultAsync().

using Windows.Devices.Sensors; using System.Threading.Tasks; public async Task SetupAccelerometerAsync() { Accelerometer accelerometer = await Accelerometer.GetDefaultAsync(); if (accelerometer != null) { // Accelerometer is available. You can now configure it. accelerometer.ReportInterval = Math.Max(10, accelerometer.MinimumReportInterval); // Set to fastest supported interval accelerometer.ReadingChanged += Accelerometer_ReadingChanged; } else { // Accelerometer not available on this device. } }

Handling Sensor Readings

Once you have an instance, you can subscribe to the ReadingChanged event to receive updates when new accelerometer data is available. The event handler will receive an AccelerometerReadingChangedEventArgs object, which contains the current reading.

void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args) { AccelerometerReading reading = args.Reading; // Access the acceleration data double accelerationX = reading.AccelerationX; double accelerationY = reading.AccelerationY; double accelerationZ = reading.AccelerationZ; // You can also get the scene-specific values if needed double accelerationX_scene = reading.AccelerationX; // For scene-based coordinate systems double accelerationY_scene = reading.AccelerationY; double accelerationZ_scene = reading.AccelerationZ; // Update UI or perform actions based on these values // e.g., UpdateUI(accelerationX, accelerationY, accelerationZ); }

Properties

Name Type Description
DeviceId string Gets the unique identifier for the sensor.
MinimumReportInterval uint Gets the minimum report interval supported by the sensor, in milliseconds.
ReportInterval uint Gets or sets the report interval for the sensor, in milliseconds. A value of 0 indicates that the default interval should be used.
ReadingType SensorReadingType Gets the type of reading provided by the sensor.

Methods

Name Description
static IAsyncOperation<Accelerometer> GetDefaultAsync() Asynchronously retrieves the default accelerometer sensor.
static IAsyncOperation<IReadOnlyList<Accelerometer>> GetDeviceSelectorAsync() Retrieves the device selector for accelerometer devices.

Events

Name Description
ReadingChanged Occurs when new sensor data is available. Handled by AccelerometerReadingChangedEventHandler.
Shaken Occurs when the device is shaken. Handled by AccelerometerShakenEventHandler.

Enums

SensorReadingType

Specifies the type of reading provided by a sensor.

Name Description
Absolute Absolute readings.
Relative Relative readings.

Best Practices

Note: The acceleration values are in units of g (where 1 g = 9.81 m/s2).