Count the User’s Steps at the Right Moment

Sinan Yılmaz
3 min readJun 30, 2021

Hi everyone from another blog post :)

We can use some Huawei Mobile Services in harmony with each other. However, in theory, we can use services that are not connected to each other together, depending on the scenario. As an example of this scenario, we can consider 2 different HMS kits. Health Kit and Awareness Kit.

Health Kit allows apps to access fitness and health data. We can develop with many data such as count of steps, speed, activity data.

Awareness Kit provides your app with the ability to obtain contextual information including users’ current time, location, behavior, audio device status, ambient light, weather, and nearby beacons.

These services do not directly exchange data with each other. However, for a suitable scenario, we can create an effective way by using both.

For example, let’s say we want to get the user’s step counts. With Health Kit we can easily do this in real-time. But we may need to know when to start/stop step counting/stopping. This is where the Awareness Kit comes into play.

Thanks to the “Behavior Awareness” feature of the Awareness Kit, we can detect the behavior of the users. In this way, we can understand that the user is taking action and start counting steps. Similarly, we can stop it if we want.

So, let’s examine how we can do all this with a simple example. After making the basic implementations of the Awareness Kit and Health Kit, we need a function for creating a new barrier. We can give a static value for the label value here. We can use these labels when starting/ending the process.

Now, we need a class extended from BroadcastReceiver to detect user behavior.

As in the code block above, we can use Behavior Awareness. The point to be noted here is BarrierStatus. The barrier status includes the current status, previous status, and last status update time. We will use the presentStatus() method of BarrierStatus as in the example. The presentStatus obtains the current barrier status. If this returns TRUE, it means that the behavior of the user under this label has started.

To take the example, we should start step counting in the TRUE state for the BEGINNING LABEL and stop it in the ENDING LABEL. We should create two functions related to this and add them to the fields I mentioned below.

Now, if we come to these 2 functions, a code block like this will help us to start the step counting process.

We can use Health Kit’s DT_CONTINUOUS_STEPS_TOTAL data type for step counting. Then, let’s start the counting process by calling the startRecord() method with the AutoRecordController from the Health Kit SDK.

startRecord() needs 2 different parameters. One is data type, the other is onSamplePointListener. Here, we can use a code block like the one below for the listener and access the step data through this listener.

We have completed the step counting process. We follow a similar path for the stop method.

As we can see in the example below, step counting kicks in as soon as we start walking. Likewise, when we stop, we detect it and stop counting.

Tips & Tricks

In this article, we performed an example of step counting. However, we can use similar processes for other scenarios of Health Kit. For example, if we want the user to create an activity record, when the user takes action, we can direct him to the Health App.

Conclusion

In this blog, we’ve looked at how we can use the Awareness Kit and Health Kit together, and how to start and stop the step counting process. I hope it was useful.

References

Thanks for reading. Don’t forget the clap if you like this blog :)

--

--