[Huawei Developers] Cagnur:
View attachment 5172527

Hello everyone,

Especially recently when we realized how important the “health” issue is for us, we learned how important it is to act in order to lead a healthy life. For the days when we worked in the home office and forgot to act like as a human, we can follow our daily steps, how many steps we have taken in 3 months and how many calories we have burned. We develop sample app to use this properties. We used these features with the help of Health Kit.

Before step into development progress, let’s examine what is Huawei Health Kit and what are the main functions.

Huawei Health Kit :

HUAWEI Health Kit allows ecosystem apps to access fitness and health data of users based on their HUAWEI ID and authorization. For consumers, Health Kit provides a mechanism for fitness and health data storage and sharing based on flexible authorization. For developers and partners, Health Kit provides a data platform and fitness and health open capabilities, so that they can build related apps and services based on a multitude of data types. Health Kit connects the hardware devices and ecosystem apps to provide consumers with health care, workout guidance, and ultimate service experience.

Main Functions :

· Data storage

Provides a data platform for developers to store fitness and health data.

· Data openness

Provides a wide range of fitness and health APIs and supports sharing of the various fitness and health data, including step count, weight, and heart rate.

· Data access authorization management

Provides settings for users and developers so users can manage developers’ access to their health and fitness data, guaranteeing users’ data privacy and legal rights.

· Device access

Provides hardware devices (including fitness and health devices) with APIs for measuring and uploading data through the standard Bluetooth protocol.

You can browse this page to examine it in more detail.

https://developer.huawei.com/consumer/en/hms/huaweihealth/

Development Process

We learned about Huawei Health Kit and its main functions. Now, we can move on development part. Primarily, we should create a project on App Gallery Connect. You can follow the steps from the link below.

https://medium.com/huawei-developers/android-integrating-your-apps-with-huawei-hms-core-1f1e2a090e98

After all these steps, we need to apply for Health Kit.

View attachment 5172529

Then, we need to select data and their permissions.

View attachment 5172531

!!! To access health data we need to authenticate the user with Huawei ID.

Let’s continue by coding Data Controller to get our users’ step data.

private fun initDataController() {
val hiHealthOptions = HiHealthOptions.builder()
.addDataType(DataType.DT_CONTINUOUS_STEPS_DELTA, HiHealthOptions.ACCESS_READ)
.build()
val signInHuaweiId =
HuaweiIdAuthManager.getExtendedAuthResult(hiHealthOptions)
dataController = HuaweiHiHealth.getDataController(context!!, signInHuaweiId)
}
}

After creating the Data Controller, we write the read Daily function to get daily steps data of our users.

fun readDaily(startTime: Int, endTime: Int) {

val daliySummationTask =
dataController!!.readDailySummation(
DataType.DT_CONTINUOUS_STEPS_DELTA,
startTime,
endTime
)
daliySummationTask.addOnSuccessListener { sampleSet ->
sampleSet?.let { showSampleSet(it) }
}
daliySummationTask.addOnFailureListener { e ->
val errorCode: String? = e.message
val pattern: Pattern = Pattern.compile("[0-9]*")
val isNum: Matcher = pattern.matcher(errorCode)
when {
e is ApiException -> {
val eCode = e.statusCode
val errorMsg = HiHealthStatusCodes.getStatusCodeMessage(eCode)
}
isNum.matches() -> {
val errorMsg =
errorCode?.toInt()?.let { HiHealthStatusCodes.getStatusCodeMessage(it) }
}
else -> {
}
}
}
}

Let’s build the request body for reading activity records. We call the read method of the Data Controller to obtain activity records from the Health platform based on the conditions in the request body. Also, we should set time format. The daily hours reading format should be like this:

” yyyy-MM-dd hh: mm: ss “

fun getActivityRecord() {

val dateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault())
val startDate: Date = dateFormat.parse("yyyy-MM-dd hh:mm:ss")!!
val endDate: Date = dateFormat.parse("yyyy-MM-dd hh:mm:ss")!!

val readOptions = ReadOptions.Builder()
.read(DataType.DT_CONTINUOUS_STEPS_DELTA)
.setTimeRange(
startDate.time,
endDate.time,
TimeUnit.MILLISECONDS
)
.build()

val readReplyTask = dataController!!.read(readOptions)
readReplyTask.addOnSuccessListener { readReply ->
Log.i("TAG", "Success read an SampleSets from HMS core")
for (sampleSet in readReply.getSampleSets()) {
showSampleSet(sampleSet)

}

}.addOnFailureListener { e ->
val errorCode: String? = e.message
val pattern: Pattern = Pattern.compile("[0-9]*")
val isNum: Matcher = pattern.matcher(errorCode)
when {
e is ApiException -> {
val eCode = e.statusCode
val errorMsg = HiHealthStatusCodes.getStatusCodeMessage(eCode)

}
isNum.matches() -> {
val errorMsg =
errorCode?.toInt()?.let { HiHealthStatusCodes.getStatusCodeMessage(it) }

}
else -> {

}
}
}
}

Bonus : I used pagination to displaying step data for better user experience and resource management.

var count = 0
var today = Calendar.getInstance()
var nextDay = Calendar.getInstance()
nextDay.add(Calendar.DATE, -20)
var dateFormat = SimpleDateFormat("yyyyMMdd", Locale.getDefault())

println("Today : " + dateFormat.format(Date(today.timeInMillis)))

stepViewModel.readToday()
stepViewModel.readDaily(dateFormat.format(Date(nextDay.timeInMillis)).toInt(), dateFormat.format(Date(today.timeInMillis)).toInt()-1)

binding.stepList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if (!binding.stepList.canScrollVertically(1) && count<5) {
count++
var otherDay = Calendar.getInstance()
var nextDay = Calendar.getInstance()

otherDay.add(Calendar.DATE, -(20*count))
nextDay.add(Calendar.DATE, -((20*(count + 1)-1)))

stepViewModel.readDaily(dateFormat.format(Date(nextDay.timeInMillis)).toInt(), dateFormat.format(Date(otherDay.timeInMillis)).toInt())
}
}
})

Finally, we completed our development process. Let’s see how it looks in our project. :)

View attachment 5172541

Conclusion

We have created an application where we can track our daily step count by taking advantage of the features provided by the Health Kit. I think it is necessary for all of us to face this reality. I hope it has been useful article for you. Your questions and opinions are very important to me.

References :

https://developer.huawei.com/consumer/en/hms/huaweihealth/