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
- Always check if the accelerometer is available before attempting to use it (i.e., check if
GetDefaultAsync()returnsnull). - Unsubscribe from the
ReadingChangedevent when your page or component is no longer active to prevent memory leaks. - Set the
ReportIntervalto an appropriate value. Setting it too low can drain battery; setting it too high might lead to a laggy user experience. UseMinimumReportIntervalto find the fastest supported rate. - Handle the
Shakenevent for simple shake gesture detection.