How to Develop a Sleep Tracker App with Health Kit?
📖Introduction
We are going to develop a simple sleep tracker application. Our application will be linked to the Huawei Health application. Sleep data will be collected from Huawei wearables such as Huawei bands, smartwatches. I will be using Huawei Band 4 to collect my sleep data for testing. We are going to display our sleep score, light sleep time, deep sleep time, rem sleep time, and finally total sleep time of the user.
✨ What is the Health Kit?
Due to today’s world, health-related applications are rising. You can see a new health application every day. Because of this Huawei offers Health Kit for developers to make it easy to develop health-related applications. Huawei describes Health Kit as “This kit incorporates apps and services as well as Huawei and third-party devices, to construct a smart health and fitness ecosystem that provides users with unmatched convenience.”. Health Kit offers us not only sleep time. Developers can get step count, sleep score, blood oxygen, and so on. Of course, they can only get and use this data if the user gives consent. You can check all data types and their information from here.
💤Introduction to Sleep
Sleep is as essential to survival as food and water. Without sleep, our body can't do repairing, form new paths inside our brain. “Research shows that a chronic lack of sleep, or getting poor quality sleep, increases the risk of disorders including high blood pressure, cardiovascular disease, diabetes, depression, and obesity.” Sleep has two main parts: rapid eye movement(REM) and non-REM sleep. Non-REM sleep also has two main parts: Light Sleep and Deep Sleep. At least we are going to use these two. To keep it short I am not going to explain sleep stages but you can read about it from the National Institute of Health documentation.
🚧 Development Process
After a brief introduction about the Health Kit and sleep, we can start our development process. First, we need to integrate the HMS Core. It is mandatory to use HMS kits and services. You can use this guide to complete the integration of HMS Core.
After this step, we need to apply for the Health Kit. To achieve this we need to open AGC and click Health Kit under the development tab.
After on the Health Kit page, click the “Apply for Health Kit” button. Select “Mobile App” as product type and also select your project name from the second selection. Finally, it is time to select the atomic data and their permissions.
Since we are developing a sleep tracker we are going to select Sleep Data from the Health Data section. Health Kit says “You need to comply with the data minimization principle and select only the required data permissions.”. So for future development keep this in mind. Also, the Restricted Health Data section needs application materials. Normally approval process takes up to 15 minutes but of course, if you selected restricted types it will take much longer. In our case, it can be instantly.
Also, we need to add Health Kit to our project in Android Studio.
🔑Authentication
To access health data we need to authenticate the user with Huawei ID. To achieve this we can use the account kit and auth service.
Besides the authorization process, we need to query our health kit authorization. According to the result, we are good to go or we need to authorize the user first.
🅾️ Data Controller
Since we are completed our sign-in and authorization part. It is time to create our data controller to get our users’ sleep data.
The data controller can insert data, delete data, update data, read data, query the statistical data of the current day, query the statistical data of multiple days, clear data of the app from the device and cloud.
We will only use our data controller to read our sleep data.
📑Read Data
Now that our data controller is ready we can start to read our sleep data. First, we will get today’s data and when the user selects a date from the calendar we will get that day’s data.
To use sleep data let’s create a sleep data model.
This will be our data model. Now we are ready to get today’s data.
As a friendly reminder, if you are getting 50005 error, you can try to silent sign in. It will fix your problems most of the time. Of course, if the user linked their account to the health app. Let’s write our second function to get sleep data from the selected date.
With the read daily function, we can get a single day or a few days together. We will only use the selected day so our startTime and endTime will be the same. Read daily time format is “YYYYMMDD”. To clear it I can say if the user selected 10 November 2020, variables will be “20201022”.
Since our data fetching functions are ready we need a function to clean our data and fill our sleep data object.
We will use the material calendar view library from Prolific Interactive. Why we are using this library instead of the native calendar view because this library’s tile size option lets us shrink the calendar size without losing the look. You can find it here. After integrating the library we need to write our function to get the selected date and call the read daily function.
Now we have our read functions, material calendar, the data controller, filtering function we only need the chart functions. For our pie chart, we are going to use the MPAndroidChart library. You can find it here. You can integrate it by simply following the documentation.
And that is it. We completed our development process and we have now a simple sleep tracker with good visuals.
💡 Conclusion
As you can see it is as easy as pie. 🥧 Normally new technologies bring advantages with a lot of things to learn but in this case, Health Kit provides advantages with only a small burden of learning. We only used continuous sleep data but we can add statistics sleep data as well to display sleep time in minutes. This was a simple sleep tracker application but it is open to improvement. An alarm clock functionality can be added, the application can send notifications for sleep time and wake-up time with Push Kit, and so on. Even adding a simple detail page will be a good option for users.