pedometer_pro 0.1.0
pedometer_pro: ^0.1.0 copied to clipboard
Pedometer & Step Detection. Get step count in a timeRange & stream live steps.
Pedometer Pro #
Based on Pedometer & Pedometer_plus (references at the bottom)
This plugin allows you to get this info on both Android and iOS:
- Get step count for a
from:todate range - Get step count since last system boot
- Get real-time step count (Stream)
- Get real-time pedestrian status: Walking, Stopped (Stream)
- Get real-time step count since a date (Stream) (iOS only; Android alternative below)
Supported on both Android and iOS. Uses the Sensors API (Android & iOS) and the Recording API (Android, when Google Play Services is available).
Package: https://pub.dev/packages/pedometer_pro
Repository: https://github.com/whevether/pedometer_pro
Android data sources #
| Capability | Typical phones (Play Store + Health Connect) | Without GMS / no Health Connect |
|---|---|---|
Real-time stepCountStream / pedestrianStatusStream |
Hardware TYPE_STEP_COUNTER / TYPE_STEP_DETECTOR |
Same sensors — supported |
getStepCount(from, to) history |
Health Connect (Samsung Health, Google Fit, and other linked apps) | TYPE_STEP_COUNTER (steps since last boot only, cannot split by calendar day) |
On aggressive OEM battery savers (Xiaomi / OPPO / vivo), background listeners may be killed. Use the streams in the foreground, and ask the user to disable battery optimization for the app if you need more reliable tracking.
Run the sample app:
cd example && flutter run
Android release signing for the example uses a committed test keystore (example/jks/). See example/jks/README.md. Do not use it for production.
Configuration #
Permissions #
For both Android and iOS you need to request permission to track the user's activity. I recommend using permission_handler, but you can use others if it suits you better.
iOS
This plugin supports both CocoaPods and Swift Package Manager. The example app uses Swift Package Manager.
- CocoaPods:
ios/pedometer_pro.podspec - SwiftPM:
ios/pedometer_pro/Package.swiftandios/pedometer_pro/Sources/
-
In your
Info.plist, located underios/Runner, add this:<key>NSMotionUsageDescription</key> <string>This application tracks your steps</string>With Swift Package Manager and recent
permission_handler, this Info.plist key is enough to enable the sensors permission. -
If your app still uses CocoaPods, also add this in
ios/Podfile:post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) ## ADD THIS SECTION target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)', ## dart: PermissionGroup.sensors 'PERMISSION_SENSORS=1', ] end ## END OF WHAT YOU NEED TO ADD end endIf you already have a
target.build_configurations.each do |config|loop in yourPodfile, only include theconfig.build_settingssection inside that loop. If you already use permission_handler, ensurePERMISSION_SENSORSis set to1instead of0.
Android
- You do not need to add
ACTIVITY_RECOGNITIONorhealth.READ_STEPSto your Android manifest. They are merged from the plugin. - Requires Android 10 (minSdk 29). Set this in
android/app/build.gradle(or.kts). - Make sure AndroidX is enabled (already true on recent Flutter projects).
- Galaxy / Samsung Health history: install Health Connect (required on Android 13, such as Galaxy S20). In Samsung Health, enable Health Connect and allow this app to read steps.
play-services-fitnessdoes not read Samsung Health. - Host apps should declare
androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALEonMainActivity(see the example app).
How to use it #
-
Request permissions
- Using permission_handler, you can request permission on both platforms.
- The plugin includes
openAppSettings()so the user can enable permissions if the system prompt is not shown again.Behavior
- The first request shows a system dialog. Later calls return the stored answer or the current OS setting.
- If permission is denied, show a snackbar or dialog and ask the user to update settings.
Example:
import 'package:permission_handler/permission_handler.dart'; PermissionStatus perm = Platform.isAndroid ? await Permission.activityRecognition.request() : await Permission.sensors.request(); print('perm: $perm'); if (perm.isDenied || perm.isPermanentlyDenied || perm.isRestricted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( 'You need to approve the permissions to use the pedometer', style: TextStyle( color: Theme.of(context).colorScheme.onError, fontWeight: FontWeight.bold, ), ), backgroundColor: Theme.of(context).colorScheme.errorContainer, action: SnackBarAction( label: 'Settings', textColor: Theme.of(context).colorScheme.onError, onPressed: () => openAppSettings(), ), ), ); } else { // Call the functions you need to read stepCount }
-
Get step count
- Request the total steps in a time range (
from/to). - If
fromortois omitted, it defaults to the maximum recorded window: 7 days on iOS, 10 days on Android. - If the range is longer than the platform window, the result only covers that window.
Behavior
- On Android, history is read from Health Connect (Samsung Health and other linked apps). The first call may show a Health Connect permission sheet.
play-services-fitnessLocal Recording is only this app's own log after subscribe, not vendor health-app history.- If Health Connect is missing or empty, ranges that include "now" fall back to steps since last boot.
Example:
import 'package:pedometer_pro/pedometer_pro.dart'; DateTime now = DateTime.now(); DateTime from = now.subtract(Duration(days: now.weekday - 1)); DateTime to = now.add(Duration(days: DateTime.daysPerWeek - now.weekday)); int steps = await Pedometer().getStepCount(from: from, to: to); print('steps: $steps');
- Request the total steps in a time range (
-
Real-time step count (Stream) and steps since last boot
- The first event is steps since last system boot. Further events stream as the user walks.
- If the value is
0, the stream may not fire until the user takes a step.Behavior
- After a reboot (or a manual date change), the value resets to
0.Example:
StreamSubscription? _subStepCount; @override void initState() { super.initState(); _listenToSteps(); } @override void dispose() { _subStepCount?.cancel(); super.dispose(); } _listenToSteps() { _subStepCount = Pedometer().stepCountStream().listen((steps) => print('Steps: $steps')); }
-
Real-time pedestrian status: Walking, Stopped (Stream)
- Returns
stoppedorwalking. On error it returnsunknown.Behavior
- The stream may not emit until the status changes. Assume
stoppedat start.Example:
StreamSubscription? _subPedestrianStatus; @override void initState() { super.initState(); _listenToStatus(); } @override void dispose() { _subPedestrianStatus?.cancel(); super.dispose(); } _listenToStatus() { _subPedestrianStatus = Pedometer().pedestrianStatusStream().listen((status) => print('Status: $status')); }
- Returns
-
[iOS] Real-time step count since a date (Stream)
- Android alternative: combine
getStepCountandstepCountStream. See the example app. - Returns steps from
fromtonow(), then keeps streaming.Behavior
- Same as
stepCountStream: first call may be0until the user walks. - iOS stores at most 7 days.
Example:
StreamSubscription? _subStepFrom; @override void initState() { super.initState(); _listenToSteps(); } @override void dispose() { _subStepFrom?.cancel(); super.dispose(); } _listenToSteps() { DateTime now = DateTime.now(); DateTime from = now.subtract(Duration(days: now.weekday - 1)); _subStepFrom = Pedometer().stepCountStreamFrom(from: from).listen((steps) => print('Steps: $steps')); }
- Android alternative: combine
Things to consider #
The APIs may be missing or behave differently on some devices:
- Some Samsung phones do not support the Sensors API.
- Older iPhones do not support Pedestrian Status.
- OEMs use different sensors and OS policies, so step totals can differ (mainly Android).
- Without Google Play Services, Android
getStepCountcannot reconstruct history from before the last reboot.
If the step sensor is not available, an error is thrown. The app must handle it.
Changelog #
See CHANGELOG.md / CHANGELOG_zh.md.
Thanks and credits #
This package was originally forked from:
Which was originally forked and inspired by:
The example app was inspired by Purrweb Agency - Dribbble.
Package status #
| Pub v. | Points | Popularity |
|---|---|---|
