https://github.com/jfversluis/plugin.maui.pedometer
Plugin.Maui.Pedometer provides the ability to read the device pedometer in your .NET MAUI application. This plugin currently only supports iOS and Android.
https://github.com/jfversluis/plugin.maui.pedometer
android dotnet dotnet-maui ios maui pedometer step-counter
Last synced: 11 months ago
JSON representation
Plugin.Maui.Pedometer provides the ability to read the device pedometer in your .NET MAUI application. This plugin currently only supports iOS and Android.
- Host: GitHub
- URL: https://github.com/jfversluis/plugin.maui.pedometer
- Owner: jfversluis
- License: mit
- Created: 2023-08-17T11:06:59.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-18T12:25:11.000Z (almost 3 years ago)
- Last Synced: 2024-11-20T03:41:51.670Z (over 1 year ago)
- Topics: android, dotnet, dotnet-maui, ios, maui, pedometer, step-counter
- Language: C#
- Homepage:
- Size: 8.19 MB
- Stars: 23
- Watchers: 3
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README

# Plugin.Maui.Pedometer
`Plugin.Maui.Pedometer` provides the ability to read the device pedometer in your .NET MAUI application. This plugin currently only supports iOS and Android.
## Install Plugin
[](https://www.nuget.org/packages/Plugin.Maui.Pedometer/)
Available on [NuGet](http://www.nuget.org/packages/Plugin.Maui.Pedometer).
Install with the dotnet CLI: `dotnet add package Plugin.Maui.Pedometer`, or through the NuGet Package Manager in Visual Studio.
## API Usage
`Plugin.Maui.Pedometer` provides the `Pedometer` class that can be used to monitor the device pedometer and track the user's step count.
### Permissions
Before you can start reading pedometer values, you will need to request the proper permissions on each platform.
#### iOS
On iOS, add the `NSMotionUsageDescription` key to your `info.plist` file. When declared, the permission will be requested automatically at runtime.
```xml
NSMotionUsageDescription
This app wants to track your pedometer readings
```
#### Android
On Android with API 29+, declare the `ACTIVITY_RECOGNITION` permissions in your `AndroidManifest.xml` file. This should be placed in the `manifest` node. You can also add this through the visual editor in Visual Studio.
The runtime permission is automatically requested by the plugin when `Start()` is called.
```xml
```
### Dependency Injection
You will first need to register the `Pedometer` with the `MauiAppBuilder` following the same pattern that the .NET MAUI Essentials libraries follow.
```csharp
builder.Services.AddSingleton(Pedometer.Default);
```
You can then enable your classes to depend on `IPedometer` as per the following example.
```csharp
public class StepCounterViewModel
{
readonly IPedometer pedometer;
public StepCounterViewModel(IPedometer pedometer)
{
this.pedometer = pedometer;
}
public void StartCounting()
{
pedometer.ReadingChanged += (sender, reading) =>
{
Console.WriteLine(reading.NumberOfSteps);
};
pedometer.Start();
}
}
```
### Straight usage
Alternatively if you want to skip using the dependency injection approach you can use the `Pedometer.Default` property.
```csharp
public class StepCounterViewModel
{
public void StartCounting()
{
pedometer.ReadingChanged += (sender, reading) =>
{
Console.WriteLine(reading.NumberOfSteps);
};
Pedometer.Default.Start();
}
}
```
### Pedometer
Once you have created a `Pedometer` you can interact with it in the following ways:
#### Events
##### `ReadingChanged`
Occurs when pedometer reading changes.
#### Properties
##### `IsSupported`
Gets a value indicating whether reading the pedometer is supported on this device.
##### `IsMonitoring`
Gets a value indicating whether the pedometer is actively being monitored.
#### Methods
##### `Start()`
Start monitoring for changes to the pedometer.
##### `Stop()`
Stop monitoring for changes to the pedometer.