Skip to main content

Integrating Huawei HMS Core to an Existing Android Application

In order to handle health fitness data

In this article, I explain how to integrate Huawei HMS Core into both existing and new android applications. This is possible as Huawei continues to use the open source core Android operating system on its devices.

Huawei health kit provides data such as Google fitness data. It opens 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 a sharing mechanism for consumers to authorise and revoke authorisation as well.

 

 

Image 1 Cloud Data Sync

 

 

Cloud API

Open capabilities on the cloud side support both web 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 authorisation.

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.

 

 

Image 2 Huawei

 

 

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

 

 

Image 3 console Huawei

 

 

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

 

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

 

 

Image 4 Maven

 

 

 

Image 5 Maven

 

 

5. Add below dependencies to app level build.gradle

 

 

Image 6 build.gradle

 

 

apply plugin: 'com.huawei.agconnect'

 

 

Image 7 gradle

 

 

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

 

 

Image 8

 

 

buildscript {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}

 

 

Image Maven

 

 

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" />

 

The app id can be obtained from your Huawei console area.

 

 

Image 10 App id

 

 

7. Check if your device has Google services available.

If you are going to integrate Huawei HMS to an existing android project, then you have to check whether Google services are available in your device. If not, continue step 8 onward.

 

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 Sign-In" services

 

 

9.Getting the required 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 the application to Huawei Health Kit.

 

Image 11 Health Kit

 

 

Huawei Health Kit provides APIs to read, add, delete, and modify 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.

Hope you liked this post. Stay tuned for more! 

 

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


LEAVE A COMMENT







POST COMMENTS


© 2021 Creative Software. All Rights Reserved | Privacy | Terms of Use