Pedometer implementation using Harmony OS Lite wearable
Harmony OS is a future-proof distributed operating system open to you as part of the initiatives for the all-scenario strategy, adaptable to mobile office, fitness and health, social communication, and media entertainment, to name a few.
Unlike a legacy operating system that runs on a standalone device, Harmony OS is built on a distributed architecture designed based on a set of system capabilities. It is able to run on a wide range of device forms, including smartphones, tablets, wearables, smart TVs and head units.
In this article, we will create a simple pedometer application for lite wearable which will count each step user takes, distance covered and Heart rate. Also, for every 1000 steps completion, wearable device will vibrate.
Pedometer app will have 2 UI screens, first screen will have start button. Once user clicks on Start button, app will route to second screen which will show STEPS, BPM (heart rate) and METER/KM (total distance) covered.
Requirements
1) DevEco IDE
2) Lite wearable simulator (or lite wearable watch)
Implementation
First page, index.hml contains start button.
index.css has style defined for first page.
index.js contains the implementation of start button. Once user clicks on start button, app will route to second page stepcounter.hml
Second page, stepcounter.hml contains UI design for displaying step count, heart beat count and total distance covered.
stepcounter.css has style defined for second page.
stepcounter.js has implementation of various APIs.
1. Body state API
This API is used to listen for changes of the sensor wearing state. We will use this API to know if user is wearing wearable device or not.
2. Heart Rate API
If body state API returns true, we will call heart rate API.
3. Step counter API
This API is used to listen for changes of step counter sensor data.
4. Vibration API
This API is used to trigger device vibration. For every completion of 1000 steps, we will call this API to vibrate wearable device.
To calculate the distance covered , we will just multiple each step with 0.762 meter.
Finally, we need to unsubscribe all sensor APIs on onDestroy()
stepcounter.js code snippet:
Permissions
If you are using real wearable device, we need to provide following permissions for Step counter and heart rate in config.json.
Tips and Tricks
If you are using simulator for development, you can define the sensor data by clicking hamburger menu.
Conclusion
In this article, we have learnt how to create simple pedometer app using various sensor APIs such as Step counter, Heart rate , On-body state and vibrator provided by harmony OS.