doc/articles/features/accelerometer.md
[!TIP] This article covers Uno-specific information for Accelerometer. For a full description of the feature and instructions on using it, see Use the accelerometer.
Windows.Devices.Sensors.Accelerometer class allows measuring the linear acceleration of the device along three X, Y, and Z-axis.| Feature | Windows | Android | iOS | Web (WASM) | macOS | Linux (Skia) | Win 7 (Skia) |
|---|---|---|---|---|---|---|---|
GetDefault | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
ReadingChanged | ✔ | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
Shaken | ✔ | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
ReportInterval | ✔ | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
GetDefault method is available on all targets and will return null on those that do not support Accelerometer or devices that do not have such a sensor.ReadingChanged and Shaken events when you no longer need the readings, so that the sensor is no longer active to avoid unnecessary battery consumption.ReadingChanged event readings.ReadingChanged and Shaken events are attached and the user sets the ReportInterval to a high value, the ReadingChanged event may be raised more often than requested. This is because for multiple subscribers to the same sensor, the system may raise the sensor events with the frequency of the one with the lowest requested report delay. This is, however, in line with the behavior of the WinUI Accelerometer, and you can filter the events as necessary for your use case.ReportInterval property on WASM is not supported directly, and we use an approximation in the form of raising the ReadingChanged event only when enough time has passed since the last report. The event is actually raised a bit more often to make sure the gap caused by the filter is not too large, but this is in line with the behavior of the WinUI Accelerometer.var accelerometer = Accelerometer.GetDefault();
accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
private async void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
// If you want to update the UI in some way, ensure the Dispatcher is used,
// as the ReadingChanged event handler does not run on the UI thread.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OutputTextBlock.Text = $"Sensor reading is " +
$"x = {args.Reading.AccelerationX}, y = {args.Reading.AccelerationY}, z = {args.Reading.AccelerationZ}, " +
$"timestamp = {args.Reading.Timestamp}";
});
}
var accelerometer = Accelerometer.GetDefault();
accelerometer.Shaken += Accelerometer_ReadingChanged;
private async void Accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ShakenTimestamp = args.Timestamp.ToString("R"));
}
accelerometer.ReadingChanged -= Accelerometer_ReadingChanged;
accelerometer.Shaken -= Accelerometer_ReadingChanged;