Integration HUAWEI HMS Core to current android application and handle Huawei health fitness data

Chirath Perera
4 min readNov 28, 2020

In this article, I explain how to integrate HUAWEI HMS Core into both existing android application and new android application. Huawei continue the open source core android operating system on its devices. You can add HUAWEI HMS Core to existing android application or a new android application.

Huawei health kit provides data such as google fitness data. It render open fitness and health data for developers, offering data aggregation, storage, and sharing capabilities for fitness and health apps and devices. It provides open APIs to read and write data and provides a sharing mechanism for consumers to authorize and revoke authorization.

Cloud API

Open capabilities on the cloud side support both web apps and phone apps. Developers can use RESTful APIs to access the Health Kit data platform. The app can read and write users’ fitness and health data upon the data-type-specific user authorization.

  1. You have to create an application in Huawei console area. https://developer.huawei.com/consumer/en/console.
  2. Download agconnect-services.json file. Go to Project setting > General manager in console.

3. Add downloaded agconnect-services file into app’s root directory in your code base.

4. Add below maven dependency to project level build.gradle.

maven { url 'https://developer.huawei.com/repo/' }

5. Add below dependencies to app level build.gradle.

apply plugin: 'com.huawei.agconnect'
maven { url 'https://developer.huawei.com/repo/' }
buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}
implementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
implementation "com.huawei.hms:health:5.0.4.300"

6. Add meta tag into your manifest file.

<meta-data
android:name="com.huawei.hms.client.appid"
android:value="your-app-id"
/>

you can get the app id from your Huawei console area.

7. Check your device has availability of google services.

If you are going to integrate Huawei HMS to current android project then You have to check whether google services are available or not in your device. If not you don't have to check this.

int resultCode =  GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.currentContext);
if (resultCode == ConnectionResult.SUCCESS) {
add google services code here
} else {
add Huawei code here
}
;

8. Adding Huawei Init and Huawei SignIn services

9.Getting steps from Huawei Health

private class GetSteps extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {

String ste = "0";

HuaweiServices huaweiServices = new HuaweiServices(getActivity());
if (huaweiServices.isGooglePlayServicesAvailable()) {
DataReadRequest readRequest = queryFitnessData(0);
DataReadResult dataReadResult = Fitness.HistoryApi.readData(mApiClient, readRequest).await(1, TimeUnit.MINUTES);
ste = printData(dataReadResult);
sendLatestStepsToServer(ste);
} else {
HiHealthOptions hiHealthOptions = HiHealthOptions.builder()
.addDataType(com.huawei.hms.hihealth.data.DataType.DT_INSTANTANEOUS_HEIGHT, HiHealthOptions.ACCESS_READ)
.addDataType(com.huawei.hms.hihealth.data.DataType.DT_INSTANTANEOUS_HEIGHT, HiHealthOptions.ACCESS_WRITE)
.addDataType(com.huawei.hms.hihealth.data.DataType.DT_CONTINUOUS_STEPS_DELTA, HiHealthOptions.ACCESS_READ)
.addDataType(com.huawei.hms.hihealth.data.DataType.DT_CONTINUOUS_STEPS_DELTA, HiHealthOptions.ACCESS_WRITE)
.build();
AuthHuaweiId signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(hiHealthOptions);

DataController dataController = HuaweiHiHealth.getDataController(getActivity(), signInHuaweiId);


Task<SampleSet> todaySummationTask = dataController.readTodaySummation(com.huawei.hms.hihealth.data.DataType.DT_CONTINUOUS_STEPS_DELTA);
todaySummationTask.addOnSuccessListener(new OnSuccessListener<SampleSet>() {
@Override
public void onSuccess(SampleSet sampleSet) {
logger("Success read today summation from HMS core");
isLinkedHuawei = true;
if (sampleSet != null) {
showSampleSet(sampleSet);
sendLatestStepsToServer(steps);
}
}
});
todaySummationTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
isLinkedHuawei = true;
String errorCode = e.getMessage();
String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode));
logger(errorCode + ": " + errorMsg);
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
});

}
return null;
}



}
private String showSampleSet(SampleSet sampleSet) {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

for (SamplePoint samplePoint : sampleSet.getSamplePoints()) {
logger("Sample point type: " + samplePoint.getDataType().getName());
logger("Start: " + dateFormat.format(new Date(samplePoint.getStartTime(TimeUnit.MILLISECONDS))));
logger("End: " + dateFormat.format(new Date(samplePoint.getEndTime(TimeUnit.MILLISECONDS))));
for (com.huawei.hms.hihealth.data.Field field : samplePoint.getDataType().getFields()) {
logger("Field: " + field.getName() + " Value: " + samplePoint.getFieldValue(field));
}
}


}

When you run the application you can see a page to link your application into Huawei Health Kit.

Huawei health kit provides APIs to read, adding, deleting, modifying fitness and health data.

By integrating the HUAWEI Health SDK into their apps, developers can call the HUAWEI Health Kit APIs to write users’ fitness and health data to the data platform.
To make this happen, developers need to:

  • Register a HUAWEI ID on the HUAWEI Developers website. Then, create an app, configure the app information, and enable the HUAWEI Health Kit service on the HUAWEI Developers Console.
  • Create a project with Android Studio IDE and configure the SDK on which HUAWEI Health Kit depends.
  • Implement HUAWEI Health Kit API calling and debugging.

Reference — https://developer.huawei.com/consumer/en/doc/health-introduce-0000001053684429-V5

Read more articles on Huawei integration . Hang on for more articles 😉.

--

--