👨🏼‍💻HarmonyOS How to Read Sensor Data (Java)

Sertaç Ayhan
Huawei Developers
Published in
3 min readJun 21, 2022
HarmonyOS

Introduction

Hi folks! In this article, I will try to explain how the sensors in the device are read with Java and how we can display it on the screen in a HarmonyOS project. I hope this will help you. Enjoy!

Implementation

First, let’s get the required permissions. These are activity motion and read health data. I added my permission under reqPermissions to config.json file. Don’t forget to check the link I put down below for the other sensors and permissions in HarmonyOS ecosystem.

config.json

You can use these guides for more detail:

Sensors Overview

Permission Development Guidelines

Layout

ability_main.xml

In this layout, we have a text view for the headline, a button named subscribe to be able to reach sensors, an image view (heart shape) to represent the heartbeat, a text view named text_rate to display heartbeat value on the screen, and likewise an image view (foot shape) for step count and a text named text_rate. I used a Directional Layout, one of the easiest methods, to form this layout. This layout corresponds to LinearLayout in Android development.

MainAbility

To explain shortly, Main Ability is a class that resembles Activity in Android development in terms of structure and lifecycle.

MainAbilitySlice

To point out briefly, AbilitySlice resembles Fragment in Android development.

An ability slice’s lifecycle is bound to the Page ability that hosts it.

We will try to execute all our work on AbilitySlice.

onStart:

This callback is invoked when the system first creates the Page ability. After this callback is executed, the Page ability will enter the INACTIVE state. This callback is triggered only once in the entire lifecycle of each Page ability. You must override this callback and set the default ability slice to be displayed.

Here, we set our UI. We assigned layout equivalent to rate and step text variable. We called initViewAnnotation and initCallback functions. Then, we defined MyEventHandler and used it to send the tasks in Callback. Finally, we coded and if block to get permissions.

initViewAnnotation: The function enabling binding the data. You can use view binding as well.

initCallback : In this function, we call the Callback interface via new. We assign sensor data in the onSensorDataModified function to the variable by taking Interface with all member functions. Here, we bind the data on the screen with the getUiTaskDispatcher().asyncDispatch function and setText function in the UI thread.

onActive: It seems like the onResume method in Android development. This callback is invoked when the Page ability in the INACTIVE state enters the foreground. After this callback is executed, the Page ability enters the ACTIVE state, in which it becomes visible and interactive. The Page ability will stay in this state unless it loses the window focus upon a certain event, for example, when the user touches the Back key or navigates to another Page ability. When such an event occurs, the Page ability returns to the INACTIVE state, and the system invokes the onInactive() callback. The Page ability may move to the ACTIVE state again, and the system will then invoke the onActive() callback again. You should implement both onActive() and onInactive() for a Page ability and use onActive() to obtain the resources released in onInactive().

subscribeBodyData: Sets a callback for a heart rate sensor to receive the sensor data at a specified sampling interval.

subscribeMotionData: Sets a callback for a motion sensor to receive the sensor data at a specified sampling interval.

Conclusion

We have successfully read our sensor data. While the heartbeat gives the instantaneous number of beats, the step count shows the total number of steps taken so far.

Code Output

References

Thread Management Development Guidelines

Page Ability and Ability Slice Lifecycle

Sensors Overview

--

--