Step counter (Pedometer)

Tip

This article covers Uno-specific information for Pedometer. For a full description of the feature and instructions on using it, see Pedometer Class.

  • The Windows.Devices.Sensors.Pedometer class allows counting steps taken with the device.

Supported features

Feature Windows Android iOS Web (WASM) macOS Linux (Skia) Win 7 (Skia)
GetDefaultAsync
ReadingChanged
ReportInterval

Using Pedometer with Uno

  • The GetDefault method is available on all targets and will return null on those which do not support Pedometer or devices that do not have such a sensor.
  • Ensure to unsubscribe from the ReadingChanged event when you no longer need the readings, so that the sensor is no longer active to avoid unnecessary battery consumption.
  • ReportInterval property on WASM is currently not supported directly. Uno uses an approximation in the form of raising the ReadingChanged event, only when enough time has passed since the last report. The event is 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 Windows' Pedometer.
  • DirectionalAccuracy is not reported on iOS, so it will always return Unknown.

Platform-specific requirements

On Android, the first reading returns the cumulative number of steps since the device was first booted up. The sensor may not correctly respect the requested reporting interval, so the implementation does this manually to make sure the ReadingChanged events are triggered only after the ReportInterval elapses.

Since Android 10, your application must declare the permission to use the step counter sensor by adding the following uses-feature declaration to the AndroidManifest.xml in your project:

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

Example

Capturing sensor readings

var pedometer = await Pedometer.GetDefaultAsync();
pedometer.ReportInterval = 10000;
pedometer.ReadingChanged += Pedometer_ReadingChanged;

private async void Pedometer_ReadingChanged(Pedometer sender, PedometerReadingChangedEventArgs 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, () =>
    {
        CumulativeSteps = args.Reading.CumulativeSteps;
        CumulativeStepsDurationInSeconds = args.Reading.CumulativeStepsDuration.TotalSeconds;
        Timestamp = args.Reading.Timestamp.ToString("R");
    });
}