CMPedometer

A Flutter plugin for accessing pedometer data and pedestrian activity on iOS and Android devices. Get step counts, check walking/running status, and verify sensor availability.

This plugin uses CoreMotion on iOS and the Activity Recognition API on Android to provide accurate step counting and activity detection. It supports real-time updates and can track various motion metrics including steps, distance, floors climbed, pace and cadence (iOS only).

Features

  • πŸšΆβ€β™‚οΈ Real-time step counting
  • πŸƒβ€β™€οΈ Pedestrian activity status (walking/running/stationary)
  • ⚑ Check sensor availability
  • πŸ“± Support for both iOS and Android

Feature Support

Feature Android iOS
Sensor Availability ❌ βœ…
Pedestrian Status βœ… βœ…
Step Count βœ… βœ…
Distance ❌ βœ…
Floors ❌ βœ…
Current Pace ❌ βœ…
Current Cadence ❌ βœ…

Getting Started

Prerequisites

Android

Add the following permissions to your AndroidManifest.xml:

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

iOS

Add the following keys to your Info.plist:

<key>NSMotionUsageDescription</key>
<string>This app needs to access motion data for step counting</string>
<key>UIBackgroundModes</key>
<array>
    <string>processing</string>
</array>

Usage

See the example app for a fully-fledged example.

Below are shown basic usage examples. Remember to set the required permissions as described above. This may require you to manually allow permissions in the phone's Settings.

Permissions

You can manually request permissions using package:permission_handler:

// Request required permissions
bool granted = await Permission.activityRecognition.request() == PermissionStatus.granted;

Verify Sensor Availability

// Check if step counting is available
bool isStepCountingAvailable = await CMPedometer.isStepCountingAvailable();

// Check if distance tracking is available
bool isDistanceAvailable = await CMPedometer.isDistanceAvailable();

// Check if floor counting is available
bool isFloorCountingAvailable = await CMPedometer.isFloorCountingAvailable();

// Check if pace tracking is available
bool isPaceAvailable = await CMPedometer.isPaceAvailable();

// Check if cadence tracking is available
bool isCadenceAvailable = await CMPedometer.isCadenceAvailable();

// Check if pedometer event tracking is available
bool isPedometerEventTrackingAvailable =
    await CMPedometer.isPedometerEventTrackingAvailable();

Check Pedestrian Status

// Get current activity status
CMPedometer.pedestrianStatusStream.listen((status) {
  switch (status) {
    case 'walking':
      print('User is walking');
      break;
    case 'stopped':
      print('User is stationary');
      break;
    default:
      print('Unknown activity status');
      break;
  }
});

Basic Pedometer Data

// Listen to step count updates
CMPedometer.stepCountStream.listen((data) {
  print('Steps taken: ${data.numberOfSteps}');
  print('Distance: ${data.distance}');
  print('Floors ascended: ${data.floorsAscended}');
  print('Floors descended: ${data.floorsDescended}');
  print('Current pace: ${data.currentPace}');
  print('Current cadence: ${data.currentCadence}');
});

Contributing

Contributions are welcome! Please read our contributing guidelines to get started.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Libraries

cm_pedometer