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

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 HiHealth Kit 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.

HiHealth:

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

HealthKit:

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 Sensors 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.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" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

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.20200903071752.25682558797240139019270292759381:50510917031623:2800:2984CDD047FA933DA5D63C6FB1FFD759C682A02D0BD1411B85BEB1CDB2F15C95.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/025/494/656/5190090000025494656.20200903071752.25682558797240139019270292759381:50510917031623:2800:2984CDD047FA933DA5D63C6FB1FFD759C682A02D0BD1411B85BEB1CDB2F15C95.png[/IMG]

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

Sensors Controller

We can achieve success in the two different scenarios with the sensor controller.The first of these scenarios is to be able to access the built-in sensor that the phone contains like pedometer.You can directly call the register method of SensorsController to register a listener to obtain the data reported by the built-in sensor.

NOTE : Currently, Health Kit does not support the access of devices. If you request the access of fitness and health devices to help users collect data, use HiHealth Kit (for the integration of the HUAWEI Health app) instead.

HiHealth Kit is also applicable if you would like to obtain the fitness and health data collected by Huawei wearables or Huawei ecosystem partners for your services offered to customers.

Using the Built-in Sensor Data of the Phone

Let’s give an example about that how to obtain the incremental step count from a phone.

Firstly, we need to sign in to your HUAWEI ID and obtain the SensorsController object.

val options = HiHealthOptions.builder().build()
// Sign in to the HUAWEI ID.
val signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(options)
// Obtain the SensorsController
sensorsController = HuaweiHiHealth.getSensorsController(this, signInHuaweiId)

After that, we will register a listener to listen to the step count.To register a listener for receiving raw data reported by a specified data collector, use the SensorsController.register method.

private val onSamplePointListener =
OnSamplePointListener { samplePoint ->
showSamplePoint(samplePoint)
}
val builder = SensorOptions.Builder()
builder.setDataType(DataType.DT_CONTINUOUS_STEPS_TOTAL)
sensorsController!!.register(builder.build(), onSamplePointListener)
.addOnSuccessListener {
Toast.makeText(this, "registerSteps successed... ", Toast.LENGTH_SHORT).show()
}
.addOnFailureListener {
Toast.makeText(this, "registerSteps failed... ", Toast.LENGTH_SHORT).show()
}

When we no longer need step data, we can use the SensorsController.unregister method to unregister the listener.

sensorsController!!.unregister(onSamplePointListener).addOnSuccessListener {
Toast.makeText(this, "unregisterSteps successed ...", Toast.LENGTH_SHORT).show()
}.addOnFailureListener { e ->
Toast.makeText(this, "unregisterSteps failed ...", Toast.LENGTH_SHORT).show()
}

Using an External Bluetooth Device

In the second scenario, we will examine how data can be obtained from external bluetooth devices.Note that : Health Kit currently supports only standard Bluetooth BLE heart rate device.

Currently, Health Kit does not support reading data generated by Huawei wearables and the HUAWEI Health app. It is scheduled that the HUAWEI Health app data, including step count, sleep data, workout records, heart rate, and body fat, will be opened up in the future.

Let’s give an example about that how to obtain the heart rate data from the Bluetooth device.

Firstly, we need to sign in to your HUAWEI ID and obtain the BleController and SensorsController objects.

val options = HiHealthOptions.builder().build()
val signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(options)
sensorsController = HuaweiHiHealth.getSensorsController(this, signInHuaweiId)
bleController = HuaweiHiHealth.getBleController(this, signInHuaweiId)

After that, we need to create a bluetooth scanning callback object by using BleScanCallback.BLE devices detected during the scanning will automatically be called back to the bleDeviceInfo object.

private val mBleCallback: BleScanCallback = object : BleScanCallback() {
override fun onDeviceDiscover(bleDeviceInfo: BleDeviceInfo) {
// Bluetooth devices detected during the scanning will be called back to the bleDeviceInfo object
Toast.makeText(this@SensorControllerActivity, "onDeviceDiscover : " + bleDeviceInfo.deviceName, Toast.LENGTH_SHORT).show()
// Save the scanned heart rate devices to the variables for later use.
this@SensorControllerActivity.bleDeviceInfo = bleDeviceInfo
}
override fun onScanEnd() {
Toast.makeText(this@SensorControllerActivity, "onScanEnd Scan called", Toast.LENGTH_SHORT).show()
}
}

For now, we can start scan for available BLE devices.We need to use the BleController.beginScan method to scan for nearby BLE devices.

bleController!!.beginScan(listOf(DataType.DT_INSTANTANEOUS_HEART_RATE), 15, mBleCallback)

We can stop scanning for Bluetooth devices.We need to use the BleController.endScan method to stop scanning.

bleController!!.endScan(mBleCallback)

We can save the device to be used by SensorsController.We need to use the BleController.saveDevice method to save the device.After successfully saving the device, you can obtain the data collector objects of the device via SensorsController, for example, using the SensorsController.getDataCollectors method.

bleController!!.saveDevice(bleDeviceInfo).addOnSuccessListener {
Toast.makeText(this, "saveHeartRateDevice successed... ", Toast.LENGTH_SHORT).show()
}.addOnFailureListener {
Toast.makeText(this, "saveHeartRateDevice failed... ", Toast.LENGTH_SHORT).show()
}

We can delete a device if a BLE device is no longer used.We need to use the BleController.deleteDevice method to delete it.

bleController!!.deleteDevice(bleDeviceInfo).addOnSuccessListener {
Toast.makeText(this, "removeHeartRateDevice successed... ", Toast.LENGTH_SHORT).show()
}.addOnFailureListener {
Toast.makeText(this, "removeHeartRateDevice failed... ", Toast.LENGTH_SHORT).show()
}

We can also list all external BLE devices that have been saved.

val bleDeviceInfoTask = bleController!!.savedDevices
bleDeviceInfoTask.addOnSuccessListener { bleDeviceInfos -> // bleDeviceInfos contains the list of the saved devices.
for (bleDeviceInfo in bleDeviceInfos) {
Toast.makeText(this, "Matched BLE devices:" + bleDeviceInfo.deviceName, Toast.LENGTH_SHORT).show()
}
}

We can obtain data collectors of the device.We need to use the SensorsController.getDataCollectors method to obtain all saved data collectors.

val dataCollectorsOptions = DataCollectorsOptions.Builder().setDataTypes(DataType.DT_INSTANTANEOUS_HEART_RATE).build()
sensorsController!!.getDataCollectors(dataCollectorsOptions)
.addOnSuccessListener { dataCollectors ->
// dataCollectors contains the returned available data collectors.
for (dataCollector in dataCollectors) {
Toast.makeText(this, "Available data collector:" + dataCollector.dataCollectorName, Toast.LENGTH_SHORT).show()
// Save the heart rate data collectors for later use when registering the listener.
this@SensorControllerActivity.dataCollector = dataCollector
}
}
.addOnFailureListener {
Toast.makeText(this, "findDataCollectors failed... ", Toast.LENGTH_SHORT).show()
}

We must use the SensorsController.register method to register a listener for receiving raw data reported by a specified data collector.We will create a listener object for heart rate data. The received heart rate data will be called back to onSamplePoint.

sensorsController!!.register(
SensorOptions.Builder()
.setDataType(DataType.DT_INSTANTANEOUS_HEART_RATE)
.setDataCollector(dataCollector)
.setCollectionRate(1, TimeUnit.SECONDS)
.build(),
heartrateListener
).addOnSuccessListener(OnSuccessListener<Void?> {
Toast.makeText(this, "registerHeartRate successed... ", Toast.LENGTH_SHORT).show()
}).addOnFailureListener(OnFailureListener {
Toast.makeText(this, "registerHeartRate failed... ", Toast.LENGTH_SHORT).show()
})
private val heartrateListener = OnSamplePointListener { samplePoint ->
Toast.makeText(this, "Heart rate received " + samplePoint.getFieldValue(Field.FIELD_BPM), Toast.LENGTH_SHORT).show()
}

If we do not need to receive real-time data, we can unregister a listener.We need to use the SensorsController.unregister method to unregister the listener.

sensorsController!!.unregister(heartrateListener).addOnSuccessListener {
Toast.makeText(this, "unregisterHeartRate successed... ", Toast.LENGTH_SHORT).show()
}.addOnFailureListener {
Toast.makeText(this, "unregisterHeartRate failed... ", Toast.LENGTH_SHORT).show()
}

References:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050071661

https://forums.developer.huawei.com/forumPortal/en/home