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.Pedometerclass 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
GetDefaultmethod is available on all targets and will returnnullon those which do not supportPedometeror devices that do not have such a sensor. - Ensure to unsubscribe from the
ReadingChangedevent when you no longer need the readings, so that the sensor is no longer active to avoid unnecessary battery consumption. ReportIntervalproperty on WASM is currently not supported directly. Uno uses an approximation in the form of raising theReadingChangedevent, 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.DirectionalAccuracyis not reported on iOS, so it will always returnUnknown.
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");
});
}