Developing Calorie Tracker App with Huawei Health Kit

Abdurrahim Çillioğlu
Huawei Developers
Published in
6 min readJan 15, 2021

Hi everyone, In this article, we will develop a simple calorie tracker app with Huawei Health Kit using Kotlin language in Android Studio.

What is Huawei Health Kit?

It is a free kit that allows users to store fitness and health data collected by smartphones or other devices like smartwatches, smart bracelets, and pedometers. Besides, these data can be shared securely within the ecosystem.

Main Functions of Health Kit

Data Storage: Store your fitness and health data easily.

Data Openness: In addition to offering many fitness and healthcare APIs, it supports the sharing of various fitness and health data, including step count, weight, and heart rate.

Data Access Authorization Management: Users can manage the developer’s access to their health and fitness data, guaranteeing users’ data privacy and legal rights.

Device Access: It measures data from hardware devices via Bluetooth and allows us to upload these data.

Features of Health Tracker App

Our application consists of two screens. We can log in to our app by touching the “Login in with Huawei” button on the first screen through Huawei Account Kit. The next screen includes areas where we can add our “calorie” and “weight” information. We can graphically display the information we’ve recorded. And, we used the free library called MPAndroidChart to display it graphically. You can access the source codes of the application through this link via Github.

1- Huawei Core Integration

First, we need to create an account on the Console. And then, we should create a project and integrate it into our implementation. We can do this quickly by following the steps outlined in the link below, or we can do it with the official codelab.

2- Huawei Health Kit Integration

We need to apply to the Health Kit service. After you log in to the Console through this link, click “Health Kit” as displayed in the image below.

Then we click on the “Apply for Health Kit” to complete our application.

We will request permission to use the data in our application. We will use “weight” and “calorie” data. So, we only ask for necessary permissions: “Access and add body height and weight” and“Access and add calories (include BMR)”

Then click the “Submit” button and finish all our processes.
Note: You’ll see that some of the options here are locked because they’re sensitive data. If you want to use sensitive data in your application, you can send an email titled “Applying for Health Kit open permissions” to “hihealth@huawei.com” They will reply to your email as soon as possible. You can get more detailed information from this link.

After getting the necessary permissions on the console, let’s turn Android Studio to continue developing our app.

build.gradle(project) -> Add the required dependencies to the project-level build.gradle file.
Note: We added jitpack url for graphics library.

maven {url ‘https://developer.huawei.com/repo/'}
maven { url ‘https://jitpack.io' }

build.gradle(app) -> Open the build.gradle (app level) file. The following dependency will be sufficient for the Health Kit. But we will add the dependencies of the Account Kit and the graphics library.

Lastly, open the AndroidManifest.xml file and add the app id into Application tags as metadata info. There are two ways to learn our app id. The first one is: go to the Console, click the “Huawei Id” under the Development Section, then select your project and you will see the app id.
The second way is that you can find it inside “agconnect-services.json” file.

3- Developing the Application

Health Kit provides us 3 APIs. These are:

DataController: To perform operations such as data adding, updating, deleting, and reading on the fitness and health data.
ActivityRecordsController: To write activity records into the platform and update them.
AutoRecordController: To read real-time Fitness and Health data

We use DataController to process calories and weight data in our application. We already mentioned that the Health Kit is safe. We ask the user to permit us to use their health data.

activity_main.xml contains the logo, application name, an input button, as shown in the above screenshot.

MainAcitivity.kt contains the necessary codes for the login process.

You should also make sure that you have added the permissions of the data as Scope. The user will see an authorization page when clicked the log in button. And, the authorization page displays permissions in the Scope field. These permissions are not marked by default so, the user should mark them.

On the CalorieTrackerActivity page, we can add and view our calorie and weight information.

activity_calorie_tracker.xml contains the design codes for Calorie Tracker Page.

We have already introduced Data Controllers. Now, let’s create a Data Controller and write permissions for the data we’re going to use.

Using the addConsumedCalorie method, we can record our data through the Health Kit. But to do this, we need to set a time interval. Therefore, we entered the current time as the end time and a second before it as the start time.

We created a method which name is addWeightData, similar to the addConsumedCalorie method. But this time, the values we entered as the start time and end time must be the same. Otherwise, when we try to enter the weight information, the application will crash. We also changed the data types.

Let’s read the calories we consumed with the readConsumedData method. We’ve chosen a time range from a week ago to the current time. Then we’ve retrieved all the data in this time range and put it on the Map as a time-value. Lastly, we called the showCaloriesWeekly method to show these data in the Bar Chart.

We also use the readWeightData method to retrieve recorded weight information.

We use the showCaloriesWeekly method to get last week’s data as a time-value. After getting values, we sum the data for every day in the last week. Finally, we call the initBarChart method to show our daily data on the Bar Chart.

showWeightWeekly works almost like the showCaloriesWeekly method. The only difference between them is that we don’t sum all the values for every day in the showWeightWeekly method. We only get the last value for every day.

InitBarChart displays our data in graphical form.

Extra

Besides adding and reading health & fitness data, Health Kit also includes to update data, delete data, and clear all data features. We didn’t use these features in our application, but let’s take a quick look.

updateWeight -> We can update data within a specified time range. If we want to use the weight information, we should give both sections the same time value. But If we would like to update a calorie value, we can give it a long time range. Additionally, It automatically adds a new weight or calorie value when there is no value to update at the specified time.

deleteWeight -> It deletes the values in the specified range.

clearHealthData -> It deletes all the data in the Health Kit.

Conclusion

In summary, we developed a calorie-weight tracking application called “Health Tracker” with the help of the Huawei Health Kit. We experienced how to add, read, update, and delete health & fitness data. Please do not hesitate to ask your questions as a comment. You can also ask your questions via the Huawei Developer Forum.

Thank you for your time and dedication. I hope it was helpful. See you in other articles!

Github Repository

References

--

--