The complete guide to step counters on Android

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's going on welcome to another episode of Oh God I just got hired as an Android programmer and I have no idea what to do as you can hear my I'm sick but I wanted to make the video because it's really interesting so this is supposed to be the complete guide for implementing a step counter on Android and step counters on Android in general so naturally at your job or somewhere someone's going to ask you to implement a step counter that you can have in your in your phone on an app where you can hold the phone and walk and you can see the steps being incremented so also naturally is for you to want to go to google fit to achieve that so because google fit offers a step counter but it's not exactly what you'd think so let's let's look at google fit here here's the device apps here is google fit alright now this is enabled use location okay use location this is already enabled and nine to two calories all right along with the SEP counter someone's gonna ask you to do as all the other apps do which is calories distance and actual steps so if you see here that's google fit here is runtastic runtastic speedometer alright they offer calories step frequency distance and speed that's fine but also steps so calories and step frequency are for the pro version for some reason it's a app we'll talk about that later but we're just seeing what all the other apps offer so you'd know how to go about it so that's fantastic and that's google fit and here's s health okay this is as health also distance calories and steps alright so those are three options which are already out there but if you see google fit if we start with google fit we're going to hold the phone as such this is projected on the screen whatever I'm seeing here you see here we're going to shake the phone you're going to be doing a lot of this if you're trying to implement a step counter all right not much is being updated really and if we go here to the timeline you can view by active time distance calories and steps let's go to the calories let's see where the nine to two came from okay show short activities nine to two inactive calories nine to two calories all right what about the active calories so over here you can see a breakdown of the calories it's not really clear first of all the implementation of google fit into your app is very convoluted very convoluted I I went through it the whole thing it's it's complex there's several ways to call the API and get get data from it and most of it is on Stack Overflow already but the way that you implement it and you have to handle how the Google API manager is closed or open or when you're going to initialize it when you're going to close it when you're going to disconnect you have to handle all of that it's a big chunk of code for no reason really I really don't like it and they do not offer you the active calories which is what we're interested in all the other apps you're seeing fantastic and s-health are showing you the calories that you're burning these are called active calories inactive calories are the ones that you're not burning I don't know what the difference is but I do know that this number should be incrementing and google fit does not allow you to have the inactive I mean the active calories you get the goal according to what google fit believes that your goal for burning calories per day is that's the number you get you get the goal you don't get the calories that are being calculated by google fit that's for calories let's go to the steps we're going to start by dissecting what google fit actually does because that's the natural first option you're going to go to oh i just use Google fit I can get everything from there amazing so there's ok let's take this day there's 164 steps ok and there's 164 miscellaneous steps so in the description for google fit and how it calculates the steps they send the value to the server so you they don't count it they do count it on the phone but you don't get to see that value so let me show you just this is a study a little study that I did on the way the google fit actually works so you see here there's it's 3:40 p.m. up top here that's 3:40 p.m. 90 steps walking is updated this at 9:15 all right so and another 40 steps miscellaneous but what about the real steps why didn't it update from 915 to 340 that's more than more than three hours with no updates of anything that's happening that is totally unreliable at all this this cannot work time difference lag also it's 518 it updated at 318 12:00 p.m. there were zero steps before that there were 49 wet wet there is an issues about this reported all over Stack Overflow and I think this is like this is typical of Google I believe it's a half implementation at best it's such a minimal app google fit I don't like to implement at all it's very inaccurate they send the stuff to the server and there's a lot of time difference and a lot of difference in values by the time you see something and you update the app again you see a different value that is really unreliable and you cannot base your any real production stuff on this I know there's maybe a lot of effort went into this but for nothing it's unusable so that was for Google for me to find this out it took me like I don't know three weeks to really lose your complete hope and Google fit because that was the first resource that we resorted to so that Google fit alright let's go to s health my dad is talking to me how can I shut this off arm arm alarms only okay no I want to be off I don't care ok let's go to s health this is from Samsung and they do have an API digital health it's it acts like a store for health data and it's the way that Google fit behaves is the same thing you push to the store you get the data you put the permissions whatever ok and you add your your your metric as a widget on their dashboard it doesn't matter let's just get the raw stuff so these are today's steps with google fit we shook the phone nothing happened alright the purpose is to be able to hold the phone shake the phone walk around see updated values that's the purpose if you don't have that you didn't implement the step counter very easy so we're going to shake the phone let's see how fast it updates it's pretty good very few local we're just shaking very slowly now still being fooled a lot so you get the idea and you get the calories burned here and you get the distance here but the issue with Samsung is that it's not very straightforward to get their code to get the way that they're calculating stuff so you have to implement the API and they have to push to apply for a partner app no just no it's it's not the easiest route to go by okay it's doable I tried but it's it's not the easiest way to go about it okay but this is accurate at least it's somewhat accurate you shake the phone you get proper numbers good enough numbers and it calculates the distance covered and calculates the calories there's another one we said we mentioned about runtastic pedometer here it is let's see how this one works start work out we're going to pick up the phone this is way sensitive reading okay too much I'm just tapping on the phone here not really walking what the this is what I'm done just tapping on the phone that's a step that's not a step this is okay this is why it should I don't know maybe they're happy runtastic is an app is a great app everybody uses it it's actually look at fantastic here what stick I need something in my mouth here it is on the store they have a million apps same shipped 89,000 ratings Wow yeah five million to 10 million installs good job but it's a vicious step counter the phone is not even moving okay I'm just tapping on it it's not even flinching what the this is an accelerometer implementation it says that if you went from speed to speed very quickly this is a step so if you go like what now alright this is an accelerometer implementation it's much worse than s health okay but it also offers you the distance and offers you the speed and the calories as we talked about so this is about the same road that we want to go show the steps show the calories burned and the distance covered so by now you cannot access runtastic scold google fit is crap as health you cannot access their code and going to the s health about google fit actually it's not something bad it's just that google fit is not meant for this specific purpose to offer you the steps i you're walking they filter out the steps for stuff that's not relevant they filter them out watch let's Google something Google fit-step steps in accurate see what we were saying happiness alert no help okay API reference we're good for Android API is REST API maybe it's here they mentioned specifically that if if they don't find any relevant activity to the steps that they're measuring they place them under miscellaneous so it's something that is not does not have anything I don't I can't find it's right right now but I have read that and if they don't find any relevant activity do the steps that they're counting they send them back to the server if they don't find anything relevant they just place them under miscellaneous and they don't count them with the total number of steps so live counting forget about it that's not the purpose of Google Chat Noir for distance nor for calories that's not the purpose of google fit not for that aside from that I haven't really tried it but I read a ton of about people saying ton of from people saying that it's highly inaccurate and they compared it with other apps and whoever is going to hire you to do this or going to ask of you to do this is going to open other apps and they're going to be checking is this accurate today at least think up in a margin of steps at least they don't have to match of course because it's not the same store or the same way of measuring but do they at least come into the range of one another if I count with your app and then I count and I go to as health who's already measuring are they even close that's how they they're going to measure they're going to do this for sure that's how they're going to measure if you if your implementation is correct or not this is for them correctness this is the measure of correctness for someone who's not a developer and just holding your app for the first time so naturally about this time we're going to be like okay we're going to roll our own how we're going to do that and any professional programmer equal to github whatever your problem is you put it in the search box and the solution appears oh see right here Android pedometer one two okay let's try the first two this guy and this guy I actually started with this guy bad G lady whatever the is that I mean that is no maintenance intended you're they're not going to maintain it but alas I did look through it either way and I implemented it let's look at it this guy has an app that speaks to you speaking timer speed notes fire step buzzer step detector step this player step listener step service here's the step service let's look at that okay is initializing the callbacks not callbacks I mean the detector from his other class accelerometer this is an accelerometer implementation okay okay all right callbacks here receive them you receive them over at you receive them in the activity where's the activity pedometer to Java okay step step changed changed there we go you get the callback for the service and you send a message to your handler step to change you go over here that's not the handler this is the handler you get the value from argh and you place it on the on the counter in your view let's see on a visor here raise visor where is his implementation oh I probably removed it but he has an app which you can use to test out all of this where is the app there it is we're gonna do this okay let's try his app download okay um what downloads a ADB installed pedometer cut APK and we're back to visor okay here's this app amazing steps miles steps per minute that's base miles per hour speed calories burned go to the options sensitivity extra high very high and nothing is selected it doesn't it starts at um value I know I know I looked at the code but it's it's not reflected here operational level for the server's nothing selected either how aggressive how non aggressive it should be step length the average body weight this is what's used to calculate the calories if you can see in the code alright and this is the the options for speaking when it should tell you if you've overdone something I don't know let's just test this okay holding phone add some value that we don't know for the sensitivity very sensitive that we go like tap the phone garbage Wow it's worse than runtastic okay this is at a certain sensitivity now if you go higher I mean lower low alright let's go again forget about forget about forget about associate all due respect to what he did of course but it's not what you wanted I this my language is offensive a little okay okay now the sensitivity is not changing that's wrong we're almost like very low extra-low high is high sensitivity we want low sensitivity okay what the it's not changing that's what about if we restart right maybe if we go like this okay this is what's being affected the swings okay so I've tried this millions of times I put the phone in my pocket I walk and it's very it's very hard to to get a step with this guy if you tap the phone I didn't know that tapping does this it's even worse than I thought but if you put the phone in your pocket and you walk around you it's very hard when you're on low sensitivity extra low to get any steps recorded so you just avoid the issue of you don't avoid the issue of tapping it's not good at all this implementation okay because it's an accelerometer so here's a gotcha with an accelerometer if you hold the phone and you walk and you hold it still in your hand no steps will be recorded because the phone is stationary it's not recording any speed right the accelerometer is not being activated so if you just hold the phone and walk these are steps these are valid steps they should count first of all I thought that they shouldn't count just place the phone in your pocket and then it will start measuring it even doesn't do that holding the phone stationary and walking is necessary to to see if it's working these are valid steps and this doesn't do that because it's an accelerometer based implementation okay so that's pedometer from Bhaiji lady good game bro nice nice effort but it's not really what we wanted why are there no issues here might think because it's not actively being maintained how did you arrived at the formula for step detection it's completely a result of experimentation I know this is lame but this is a weekend hack other step sexual algorithm work basically a grade sensor values finds the maximum and if it's bigger than a value which depends on sensitivity and it comes to this step there's some additional optimizations are ok completely it's not it's not reliable at all okay this is from modular V now here's the other implementation javelin this is very good this is the one I settled with actually before we go on now you see what it takes to take a custom implementation let's look at this chart it's a very nice chart that I made to compare so you have these four candidates google fit as health runtastic our own custom solution so google fit does not offer any speed measurement it offers distance and calories and steps but all three of those are not live tracking enabled that's what LT stands for and they are infrequently updated so you might wait two hours you might wait three hours sometimes more as you saw with the screenshots before they won't get updated so live live tracking forget about it google fit is not the solution and doesn't offer speed or pace as health does not offer read everything else it does offer and it is live Trax ran tastic only offers these four I mean oh yeah only offers these three steps distance and speed and the calories and pace are paid only it is live tracking but their tracker is garbage our custom solution offers speed you can if you get a proper step counter you can calculate speed distance same thing calories same thing pays same thing steps also so with the custom solution you just have to nail the step counting the rest are values that can be calculated okay I have not yet done that because I just settled on a new step counter which is from javelin the sky which is not written by him you'll see in a second and I still have yet to implement the rest of the metrics based on this new step counter because now I I have a reliable step counter which you'll see in a moment um okay here's the source it's not like some black magic you will see what he did sensor listener this is a service on sensor changed he saves to the database whatever wait a minute yup okay and saves to the database he he updates the widgets because he has the widget he has a widget outside so this is stuff that has nothing to do with what we're interested in what we're interested in is the following I'm sorry about my voice it's just really registry registered sensor sensor manager get system service sensor service type step counter this is from Google okay this is the sensor that he implements he does it in a service he also does it here here it's as easy as this okay and you have your uncensor changed where is it you have your own uncensor changed and you have your own accuracy changed this never gets old ever it always returns the same value since boot the caveat with this I mean the gotcha is that the sensor inside your device this is only by the way it's only API 18 and onwards okay so KitKat and above/below no one has the sensor on their device okay you you should use use uses feature to guarantee that it doesn't show up for devices that are not enabled to handle it okay so this implementation is so easy to do and it's very accurate let's let's actually check it out this is my this is the app that I've implemented it in this is this was 166 before I started the video because we shook the phone a lot this has been recording in the background so let's actually test the tapping on the phone this is the same action that I was doing with all the other apps tapping on the edge of the phone obviously with good reason nothing happens but if you roll and swing it counts so it's accumulative it's it it still counts it it's like a queue it's not it's not like instantaneous I mean is it synchronous no it's asynchronous I think so the caveat with this though is that it counts the steps sends phone booth so you gotta be careful since phone booth if you restart the phone that's when the values you get reset if you don't they're still going to be the same values no matter what you do even if you uninstall the app and reinstall they're going to be the same values because the sensor stored the value all right you're just subscribing to the sensor when you implement it on the device if it was implemented once at least it will start storing and it will have the same value even if you uninstall the app but it's very accurate and very reliable I've tested it millions of times just like the all the rest you put this in your phone you hold this in your hand you walk around you start counting how many steps you stopped you will see this app the same number regardless of what the original number is just implement it look at the first number that shows up add to it whatever you counted the difference I mean the result of that addition that's what you should be measuring what shows up in the beginning you can reset that whenever you want you just save the value and shared preferences and whenever this guy increments that's a step for you to add to the number that you have over there it's not very good to get this number and place it over there because sometimes you want to reset the step count at midnight okay so you can't just keep this running all the time it's you reach 90,000 in the whole lifetime that you've had the app installed because it's steps since phone boot all right so it resets the value when the phone is restarted so what you should do is whenever you hit the on sensor changed you know this is a step you add one to the count of steps that you already have okay so that's the step counter from from google fit I mean from Google you can see it implemented here this is his repository and this is my custom app that I've implemented this code within I have a service just like him let me just write down what the exact flow is of what what's happening here so you have a sensor service sensor manager sensor equal new sensor step counter this is not the exact fold I'm just saying that this is what you should do this gets you your step counter from it you'll implement sensor event listener to wherever you wanna initializes probably in the service that is running all the time from it you have your own sensor changed where you increment the number of steps and shared prefs or database every hit of this callback is a guaranteed step no duplicates so it doesn't give you the same number again ever so it's reliable that you know each time you reach this callback I don't know if that's clear enough should be very clear this point whenever you hit the callback that's a step you go increment the number that you're keeping track of okay counts steps since boot you booting the phone is the only way to reset the sensors values so that's one you have that place above and in place the above in a service probably start the service on entering the main activity actually the activity of your choice for use in updating the UI of that activity so you have your sensor manager implemented in one activity where you where you could do that sure you could do that implemented in one activity and the sensor on sensor change is going to return the same value even if you implement another sensor manager step counter the same one in another place in a service both are going to return the same activity so you should have the same number so you should have one of them updating the UI and another one running in the background always okay or you could just not do this one you could have a service running in the background all the time within it you send an event bus event that will be it received by the activity and you use that to update the UI of that activity okay otherwise you're just counting and running this steps where is this step this step here the service is just running all the time sending an event to whoever is listening in this case an activity that has to update the UI it sends that event either way even if it's in the background service and second the wall inside on sensor changed you are also incrementing the number of steps that you are keeping track of as the service is recording so if you want to see this you should check out this guys library implementation is very easy actually let me which anyone could you let me show you this is a proprietary code but whatever here's the sensor listener I implement sensor event listener on accuracy changed on sensor changed okay what I do here is the following I get the steps this is the raw value of the steps step straw is this steps in preferences is this the key of today's date okay I get the steps from preferences I store them here I get the steps from sensor you know I store I store the value of the sensor in preferences as well and I store the value of today's steps also in preferences but under a different key so that I compare them okay for first launch forget about the first launch if this sensor steps from preface is different than steps that's raw so that I guarantee this is a new value which is already going to be a new value you don't have to do this comparison or to grab this one or store it even you don't but this is just to be on the safe side if they're different you increment by one so I'm not using this one I'm not using it to store because this stores value since phone boot which is not what I want I want a new counter every day so how do I reach the every day up and this is the event that I send event step counter incremented I grab this here in the fragment I update the UI from prefs get it it's really easy to implement this is an attempt of calculating the calories this is wrong it doesn't work sat down yet and this is his own implementation it's pretty easy this is the best solution I could find given all the comparisons that I did and then once you saw this is the most reliable one from Google and here's his manifest use this feature sensor step counter okay it has a received boot completed so he can run the service on boot so you can see here there's a boot receiver just start the service whenever the phone boots again so you don't have to get into the place where it starts to service within an activity start it anyways okay where's where's the browser okay and it has a boot where is it power receiver action power papa boot completed you know implementing a service simple and let's just look at the sensor or do a step sensor there we go okay API level 20 what what no that's what I want it's supposed to be level 18 let's just see something anyways it's it's enabled for API liability and an above Android hardware sensor step counter motion sensors okay this is the one maybe Oh plus 10 ah nice what for quick um API page okay user input new stem sound what what API is this get cut oh there we go KitKat okay they added this in this so KitKat and above and what level is KitKat what level is KitKat where is it it's 19 okay sorry 19 and above okay and if you look at the distribution who has less than 19 KitKat is 19 okay everyone below KitKat makes up so jellybeans and beyond that so this is if this is 29 I don't know um 15 plus 29 and in 21 1521 14 29 so 79% have above Kit Kat Kit Kat and above and only the rest has below so that's a trade-off you have to decide whether you're willing to make for the sake of accuracy and for the sake of a good implementation really it's it's the best implementation you can try it out here's his library I'm going to link to it in the video you can try out his app it's very easy to implement three as you saw and that's it for today I hope you enjoyed and I'll catch you next time guys
Info
Channel: Dry Code
Views: 28,977
Rating: 4.5189004 out of 5
Keywords: android, steps, step-counter, google-fit, google, fitness, fit, samsung, s health, runtastic, github, programming
Id: 8Wku0jvnXEA
Channel Id: undefined
Length: 40min 54sec (2454 seconds)
Published: Sun Aug 14 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.