[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201326975480310022&fid=0101246461018590361

Hello everyone , in this article we’re going to take a look at the Huawei Health Kit features so we can easily put these features to our apps.

Health app industry have shown an important increase in recent years.The software and healthcare industry are working together to find the latest technology products for the needs of patients and families.Huawei offers high technology service for the health and fitness app ecosystem with HealthKit and HiHealthKit service.Huawei and third-party partner capabilities track and customize all aspects of better living.

Data storage : Provides a platform for users to store their fitness and health data.

Data openness : Provides a wide range of APIs for writing and reading speed, positioning, blood sugar level, and other data.

Data interaction : Enables you and your users to agree on data access, guaranteeing proper use and security of personal data.

HealthKit:

https://developer.huawei.com/consumer/en/hms/huawei-healthkit

HiHealth:

https://developer.huawei.com/consumer/en/hms/huawei-healthkit

We will examine the differences between HealthKit and HiHealth in our next articles.I created a sample app which I’ve open-sourced on GitHub for enthusiasts who want to use this service.

https://github.com/tualdev/hms-health-kit-demo

Setting up the Health Kit

You’ll need to do setup before you can use the Health Kit.You can follow the official documentation on how to prepare app and enable Health Kit on App Gallery Connect.

Also you can follow this blog post to integrate your apps with Huawei HMS Core:

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

I’ll show you how to implement Data Controller features in this article for your android projects.Let’s get started!

Add the required dependencies to the build.gradle file under app folder.We need some dependencies.

implementation 'com.huawei.hms:hwid:4.0.1.300'
implementation 'com.huawei.agconnect:agconnect-core:1.3.1.300'
implementation 'com.huawei.agconnect:agconnect-auth:1.3.1.300'
implementation 'com.huawei.hms:hihealth-base:5.0.0.300'

Add the required permissions to the AndroidManifest.xml file under app/src/main folder.

<uses-permission android:name="android.permission.INTERNET" />

You need to give some permissions after applying for HealthKit in service cards area.The applied permissions will be displayed on the user authorization page.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200813091703.42706900731688904146326273485630:50510827012708:2800:3BC0682893FCC72027F13FCA884B53B3973537F3E8D1A2C26D8D25BB0F07F58F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200813091703.42706900731688904146326273485630:50510827012708:2800:3BC0682893FCC72027F13FCA884B53B3973537F3E8D1A2C26D8D25BB0F07F58F.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200813091703.42706900731688904146326273485630:50510827012708:2800:3BC0682893FCC72027F13FCA884B53B3973537F3E8D1A2C26D8D25BB0F07F58F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200813091703.42706900731688904146326273485630:50510827012708:2800:3BC0682893FCC72027F13FCA884B53B3973537F3E8D1A2C26D8D25BB0F07F58F.png[/IMG]

Let’s continue by coding Data Controller functionalities in our project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200813091747.99781761181374008587054482973952:50510827012708:2800:37B2114B66A40632CC4F85085972C6AA2BC0AF655B851BF6ED236A8A5EB68950.jpeg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200813091747.99781761181374008587054482973952:50510827012708:2800:37B2114B66A40632CC4F85085972C6AA2BC0AF655B851BF6ED236A8A5EB68950.jpeg[/IMG]

Sign-in before using Data Controller

Firstly, we need to configure Huawei ID sign in.After that, we will start sign intent while using startActivityForResult method.We added also some scopes to apply for. The following only shows an example. You need to add scopes according to your specific needs.

huaweiIdAuthParamsHelper = HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
val scopeList = listOf(
Scope(HwIDConstant.SCOPE.SCOPE_ACCOUNT_EMAIL),
Scope(HwIDConstant.SCOPE.ACCOUNT_BASEPROFILE),
Scope(Scopes.HEALTHKIT_STEP_BOTH),
Scope(Scopes.HEALTHKIT_HEIGHTWEIGHT_BOTH),
Scope(Scopes.HEALTHKIT_HEARTRATE_BOTH)
)
huaweiIdAuthParamsHelper.setScopeList(scopeList)
authParams = huaweiIdAuthParamsHelper.setAccessToken().createParams()
authService = HuaweiIdAuthManager.getService(this, authParams)

HEALTHKIT_STEP_BOTH → View and save step counts in HUAWEI Health Kit.

HEALTHKIT_HEIGHTWEIGHT_BOTH → View and save height and weight in HUAWEI Health Kit.

HEALTHKIT_HEARTRATE_BOTH → View and save the heart rate data in HUAWEI Health Kit.

For details about the mapping between scopes and permissions, see Scope in the API reference.After successfully sign process , we can move to crud and other operations. We are able to call ten methods in DataController to perform operations on the fitness and health data.The methods include:

insert: inserts data.

delete: deletes data.

update: updates data.

read: reads data.

readTodaySummation: queries the summarized data of the current day.

readTodaySummationFromDevice: queries the summarized data of the local device of the current day.

registerModifyDataMonitor: registers a listener for data updates.

unregisterModifyDataMonitor: unregisters a listener for data updates.

syncAll: syncs data between the device and cloud.

clearAll: clears data of the app from the device and cloud.

We need to create the data controller object before starting operations.

val hiHealthOptions = HiHealthOptions.builder()
.addDataType(DataType.DT_CONTINUOUS_STEPS_DELTA, HiHealthOptions.ACCESS_READ)
.addDataType(DataType.DT_CONTINUOUS_STEPS_DELTA, HiHealthOptions.ACCESS_WRITE)
.addDataType(DataType.DT_INSTANTANEOUS_HEIGHT, HiHealthOptions.ACCESS_READ)
.addDataType(DataType.DT_INSTANTANEOUS_HEIGHT, HiHealthOptions.ACCESS_WRITE)
.addDataType(DataType.DT_INSTANTANEOUS_BODY_WEIGHT, HiHealthOptions.ACCESS_READ)
.addDataType(DataType.DT_INSTANTANEOUS_BODY_WEIGHT, HiHealthOptions.ACCESS_WRITE)
.build()
val signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(hiHealthOptions)
dataController = HuaweiHiHealth.getDataController(this, signInHuaweiId)

Inserting Data

We build a DataCollector object to insert body weight.

val dataCollector: DataCollector = DataCollector.Builder().setPackageName(this)
.setDataType(DataType.DT_INSTANTANEOUS_BODY_WEIGHT)
.setDataStreamName("BODY_WEIGHT_DELTA")
.setDataGenerateType(DataCollector.DATA_TYPE_RAW)
.build()

We will create a sampling dataset based on the data collector.

val sampleSet = SampleSet.create(dataCollector)

We will create the start time, end time, and weight value for a DT_INSTANTANEOUS_BODY_WEIGHT sampling point.The weight value must be in float format.

val dateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault())val currentDate = dateFormat.format(Date())val startDate: Date = dateFormat.parse(currentDate)val endDate: Date = dateFormat.parse(currentDate)val samplePoint = sampleSet.createSamplePoint().setTimeInterval(startDate.time, endDate.time, TimeUnit.MILLISECONDS)samplePoint.getFieldValue(Field.FIELD_BODY_WEIGHT).setFloatValue(weight)
sampleSet.addSample(samplePoint)

And in this time, we need to call the data controller to insert the sampling dataset into the Health platform.

val insertTask: Task = dataController.insert(sampleSet)
insertTask.addOnSuccessListener {
Log.i("HomeFragment","Success insert a SampleSet into HMS core")
showSampleData(sampleSet, "Insert Weight")
}.addOnFailureListener { e ->
Toast.makeText(this, e.message.toString(), Toast.LENGTH_LONG).show()
}

If you examine the showSampleData function, you can see how the added data was reached.

private fun showSampleData(sampleSet: SampleSet, desc: String) {
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
for (samplePoint in sampleSet.samplePoints) {
Log.i("HomeFragment","Sample point type: " + samplePoint.dataType.name)
Log.i("HomeFragment", "Start: " + dateFormat.format(Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))))
Log.i("HomeFragment", "End: " + dateFormat.format(Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))))
for (field in samplePoint.dataType.fields) {
Log.i("HomeFragment", "Field: " + field.name + " Value: " + samplePoint.getFieldValue(field))
Toast.makeText(this, desc + samplePoint.getFieldValue(field), Toast.LENGTH_LONG).show()
}
}
}

Deleting Data

We will build a DataCollector object for data deletion.

val dataCollector: DataCollector = DataCollector.Builder().setPackageName(this)
.setDataType(DataType.DT_INSTANTANEOUS_BODY_WEIGHT)
.setDataStreamName("BODY_WEIGHT_DELTA")
.setDataGenerateType(DataCollector.DATA_TYPE_RAW)
.build()

After creating data collector object, we need to build the time range for the deletion: start time and end time.

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

We build a parameter object as the conditions for the deletion while using DeleteOptions.So, the data in this time interval will be deleted.Also note that: Only historical data that has been inserted by the current app can be deleted from the Health platform.

val deleteOptions = DeleteOptions.Builder()
.addDataCollector(dataCollector)
.setTimeInterval(startDate.time, endDate.time, TimeUnit.MILLISECONDS)
.build()

And as last step, we use the specified condition deletion object to call the data controller to delete the sampling dataset.

Calling the data controller to delete the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data deletion is successful or not.

val deleteTask = dataController.delete(deleteOptions)
deleteTask.addOnSuccessListener {
Log.i("HomeFragment","Success delete steps data")
Toast.makeText(this@DataControllerActivity, "Delete success", Toast.LENGTH_LONG).show()
}.addOnFailureListener {
Log.v(TAG, it.message.toString())
}

Updating Data

We will build a DataCollector object for data update.

This is not the end. For full content, you can visit https://forums.developer.huawei.com/forumPortal/en/home