Comments (12)
I'm planning to do a write-up about it on my blog soon, but roughly, here's how I did it:
You'll need the following vars:
savedStepCount
to save previous days' step count (e.g using shared_preferences). If you already save and display/use/show previous days' step count, you cannot use that var for this purpose, as this savedStepCount will reset intermittentlylastDaySaved
, either an int or a DateTime to save the last time you "reset" your pedometer. this will become apparent shortly. persist this as well.value
is the count received from the pedometer streamtodayStepCount
if (value < savedStepCount) {
// Upon device reboot, pedometer resets. When this happens, the saved counter must be reset as well.
savedStepCount = 0;
// {persist this value using a package of your choice here}
}
var lastDaySaved;
// {load the saved value using a package of your choice here}
if (lastDaySaved < todayDayNo) { // whether you use int or DateTime, this should only return true once every 24 hours, otherwise your value will reset too frequently.
lastDaySaved = todayDayNo
// {save lastDaySaved here}
savedStepCount = value;
// {save savedStepCount here}
}
todayStepCount = value - savedStepCount;
return todayStepCount; // this is your daily steps value.
Hope this helps. If anyone finds a neater solution, please drop it here!
from flutter-plugins.
Migrate to androidX: https://github.com/cph-cachet/flutter-plugins/pull/40/files
from flutter-plugins.
from flutter-plugins.
from flutter-plugins.
@xuwankang steps are cumulative but reset whenever device is rebooted.
from flutter-plugins.
from flutter-plugins.
To fix this, I had to
git clone https://github.com/cph-cachet/flutter-pluginsthis
mkdir my_flutter_project_dir/packages
cp flutter-plugins/packages/pedometer my_flutter_project_dir/packages
then in my_flutter_project_dir/packages/pedometer/android/build.gradle
I changed
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2' // <- bump
}
android {
compileSdkVersion 28 // <- bump
defaultConfig {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// ^ bump to androidX
}
lintOptions {
disable 'InvalidPackage'
}
Finally in pubspac.yaml
, I changed
dependencies:
pedometer: ^1.0.0
to
dependencies:
pedometer:
path: packages/pedometer
and built my project
from flutter-plugins.
to extend on @MaskyS wonderful post, here is my implementation of his idea, with a bonus of being able to preserve today steps count over reboot
value
is the value passed directly from steps counter stream
Future<int> getTodaySteps(int value) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int todayDayNo = DateTime.now().day;
int savedStepCount = prefs.getInt('savedStepCount') ?? 0;
int lastDaySaved = prefs.getInt('lastDaySaved') ?? 0;
int todaySteps = prefs.getInt('todaySteps') ?? 0;
int lastValue = prefs.getInt('stepsLastValue') ?? 0;
prefs.setInt('stepsLastValue', value);
// device reboot
if (savedStepCount > value) {
prefs.setInt('savedStepCount', 0);
savedStepCount = 0;
}
// next day
if (todayDayNo > lastDaySaved) {
prefs.setInt('lastDaySaved', todayDayNo);
prefs.setInt('savedStepCount', value);
prefs.setInt('todaySteps', 0);
savedStepCount = value;
todaySteps = 0;
}
if (savedStepCount == 0 && todaySteps > value) {
if (value < 10) {
prefs.setInt('todaySteps', value + todaySteps);
} else {
prefs.setInt('todaySteps', value - lastValue + todaySteps);
}
} else {
prefs.setInt('todaySteps', value - savedStepCount);
}
todaySteps = prefs.getInt('todaySteps') ?? 0;
if (todaySteps > 0) {
return todaySteps;
} else {
return 0;
}
}
if (todaySteps > 0)
is an overkill to make sure I never run into some sticky situation where I display negative step count in UIif (value < 10)
is to make sure there is ten step buffor for a sensor to catch up after reboot, otherwise, it would substractlastValue
recorded before the reboot, fromvalue
which would be equal to 0 after reboot
from flutter-plugins.
Hi, the latest version of the Pedometer plugin (1.2.0) has the same behaviour on both platforms, i.e. reporting the step count since last reboot.
The idea of keeping track of the step count between boots is great but our philosophy on the plugins is that they have to be quite simple and 'lean' since there is always a wide array of use cases, for each case a different implementation would be preferred.
This means you will have to store these things yourselves, perhaps make a package (not a plugin) which uses the pedometer plugin?
from flutter-plugins.
I finally got around to writing down a full-blown code sample for tracking daily steps. Here's the blog post and the code.
from flutter-plugins.
I finally got around to writing down a full-blown code sample for tracking daily steps. Here's the blog post and the code.
Great @MaskyS but when I kill the app, the value resets, is there any solution for that
from flutter-plugins.
to extend on @MaskyS wonderful post, here is my implementation of his idea, with a bonus of being able to preserve today steps count over reboot
value
is the value passed directly from steps counter streamFuture<int> getTodaySteps(int value) async { SharedPreferences prefs = await SharedPreferences.getInstance(); int todayDayNo = DateTime.now().day; int savedStepCount = prefs.getInt('savedStepCount') ?? 0; int lastDaySaved = prefs.getInt('lastDaySaved') ?? 0; int todaySteps = prefs.getInt('todaySteps') ?? 0; int lastValue = prefs.getInt('stepsLastValue') ?? 0; prefs.setInt('stepsLastValue', value); // device reboot if (savedStepCount > value) { prefs.setInt('savedStepCount', 0); savedStepCount = 0; } // next day if (todayDayNo > lastDaySaved) { prefs.setInt('lastDaySaved', todayDayNo); prefs.setInt('savedStepCount', value); prefs.setInt('todaySteps', 0); savedStepCount = value; todaySteps = 0; } if (savedStepCount == 0 && todaySteps > value) { if (value < 10) { prefs.setInt('todaySteps', value + todaySteps); } else { prefs.setInt('todaySteps', value - lastValue + todaySteps); } } else { prefs.setInt('todaySteps', value - savedStepCount); } todaySteps = prefs.getInt('todaySteps') ?? 0; if (todaySteps > 0) { return todaySteps; } else { return 0; } }
if (todaySteps > 0)
is an overkill to make sure I never run into some sticky situation where I display negative step count in UIif (value < 10)
is to make sure there is ten step buffor for a sensor to catch up after reboot, otherwise, it would substractlastValue
recorded before the reboot, fromvalue
which would be equal to 0 after reboot
This code works fine, but want to know that ho to reset steps to 0 daily? for example, when a new day starts, the steps count should be automatically set to 0 even if i don't open the app on that day
for example after 12am, new day have started and i have taken the steps, so when i open the app, i need to see updated step count for that day...it means we have to run above function in backgroud and it should be automatically hit without opening the app, whenver new day starts.
from flutter-plugins.
Related Issues (20)
- [Health 10.2.0] Adding routes to workouts HOT 2
- [health: ^10.2.0] Permission launcher not found HOT 5
- Issue on Android showing steps 0 always HOT 1
- [Pedometer] Pedometer starts accessing device sensor list on app launch
- [health, 10.2.0 (latest)] Can the heart rate be read from a watch in real time? HOT 2
- [Health 10.2.0] Android Incompatibility with audio service
- [health 10.2.0]
- Health 10.2.0 HOT 4
- Error When Using Flutter `carp_background_location` in Flutter Background Service on iOS.
- [Health] Add UUID to HealthDataPoint HOT 7
- [health: 10.2.0] no default HealthWorkoutActivityType when no match is found HOT 5
- [health 10.2.0] Remove references to deprecated v1 Android embedding
- [HEALTH 8.1.0] Unable to retrieve steps for certain Android Device HOT 2
- [HEALTH 10.2.0] support for more Android health platforms
- [pedometer 4.0.1] Support Recording API on Android HOT 1
- NUTRITION data is crashing the app HOT 4
- [Health] Atrial fibrillation support HOT 4
- [flutter_foreground_service 0.4.1] Unsupported on Android 14 (targetVersion: 34) HOT 2
- [health] writeHealthData() saves incorrect weight to Health Connect
- Need option to reset steps.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter-plugins.