A preliminary study on Android’s step counting function

This paper mainly introduces the initial exploration of Android's step counting function and shares it with you as follows:

I have browsed a lot of step counting software on the market, but unfortunately there is no open source code. However, several open source step counting codes on GitHub are either inaccurate, or their functions are imperfect and unstable. So I am determined to write one myself and share it with you. I hope you can improve it together.

Uploaded GitHub: https://github.com/xfmax/BasePedo

Note: according to the feedback of developer friends, it is generally required to add the cross day data clearing operation, so it is added in version 1.3. The latest code is on the development branch, and some new functions will also be tested on this branch. Interested students can find bugs together.

!!!: In response to the needs of small partners, we are ready to start studying the running step counting function in 2017. Please look forward to it and welcome your attention.

basepedo.png

Project structure.png

First take a look at mainactivity:

Initialize the handler in the oncreate method, and start the service in the OnStart method for fallback to the background, and then to the foreground. The OnStart method will be triggered to start the service.

Open the service in the form of bind, so serviceconnection receives the callback.

Steps to receive callback from the server:

Next, analyze the open stepservice:

Similarly, there is also a handler in stepservice, which is responsible for communicating with mainactivity.

The oncreate method in stepservice registers off screen, on screen and other broadcasts. Start a thread to execute step counting logic.

At the same time, start a timer and write data to the database once in 30s.

In the registered broadcast, the storage time will be changed according to whether the user is in the foreground or background< code>

In onstartcommand, initialize today's steps from the database and update the notification bar.

At the same time, turn on Google's built-in pedometer and acceleration sensor. If you only need one of them, please modify it by yourself.

Next, the more important step counting algorithm, stepdcrator class:

Please note that this class implements the sensoreventlistener interface, and the instance of this class is registered in stepservice.

Next, the method onsensorchanged (sensorevent event) implemented by this interface will return the value of the sensor callback and pass it into calc_ Step (event) method, waiting for the next step.

calc_ Step method calculates the average value of the X, y and Z axes of the acceleration sensor (in order to balance the data error caused by excessive value in one direction), and then hands it to the detector newstep method for processing.

Next, detect the peaks and troughs. See the notes for details.

Let's look back at the prestep method. This method divides the pedometer into three modes through the variable counttimestate. When counttimestate = 0, it means that the pedometer has not been turned on.

Counttimestate = 1 represents the preprocessing mode, that is, temp_ If the number of step steps continues to increase within the specified time until the end of this mode, then temp_ The step value is valid. On the contrary, it is invalid and discarded. The purpose is to filter the impact of some mobile phone shaking.

Counttimestate = 2 represents normal step counting mode

The following is the method of detecting wave crest:

The threshold is dynamically generated to compare with the difference between peak and trough, and then judge whether it is step 1.

Next, let's take a look at the gradient of the threshold, take four groups of values and gradient them. How to give these gradient values? I can tell you that this is what a large number of tests have tried out.

Finally, after analyzing this, I basically combed the general process. The above is all the content of this article. I hope it will be helpful to everyone's learning and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>