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.
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.
maven { url 'https://developer.huawei.com/repo/' }
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"
<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.
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 };
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.
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:
Hope you liked this post. Stay tuned for more!
Reference — https://developer.huawei.com/consumer/en/doc/health-introduce-0000001053684429-V5
LEAVE A COMMENT