With the improvement of people's living standards, people pay more and more attention to health. When using some health apps, users not only want to know the basic information such as height and weight, but also want to know some daily data such as heart rate and blood oxygen, which is convenient to follow at any time. own health. At this time, the App needs to pay attention to and record health data every day, such as daily diet, sleep habits, heart rate, blood pressure, blood sugar changes, and exercise data, and establish a personal health file to view health data for a period of time, such as a week or a month. Change trends, pay attention to abnormal data at the first time, so as to seek medical treatment or adjust living habits in time. As a result, users have more control over their health and doctors can diagnose more accurately.
So for health apps, how to achieve the above functions? Huawei Health Kit opens up basic capabilities for developers. By integrating the basic capabilities of Health Kit, you can quickly build basic functions for the App, such as reading, inserting, deleting, and updating one-day or multi-day health records, bringing users a high-quality experience.
By accessing the basic capability service of Health Kit, the developer app can obtain the user's cloud-side health data on the Huawei Sports Health App after the user's authorization, and display it to the user on the app.
Effect example
This Demo is modified on the basis of the sample code of the basic ability service of sports health service. Interested developers can download and experience optimization.
development preparation
- Registering an account and applying for an account service : Because the Health Kit service requires the ability of the account service, you need to apply for the HUAWEI ID service before you can apply for the Health Kit service.
- Apply for Health Kit Service : Apply for the data read and write permissions required by the product. Find and apply for Health Kit services in Development Services. Check the data permissions that the product needs to apply for. Here, the height and weight we need to apply for are unrestricted data. After submitting the application, they will be quickly reviewed and approved. Heart rate, blood pressure, blood sugar, and blood oxygen saturation are restricted data and need to be manually reviewed. .
- Integrate HMS Core SDK : Before developing an application, the SDK that extends the service capabilities of Health Kit needs to be integrated into the development environment.
Open the project project with Android Studio, find and open the build.gradle file in the root path of the project project. Add the maven repository address of the SDK in "allprojects > repositories" and "buildscript > repositories".
maven {url 'https://developer.huawei.com/repo/'}
Open the application-level build.gradle file under the app, and add the following compilation dependencies in "dependencies".
implementation 'com.huawei.hms:health:{version}'
Re-open the modified build.gradle file, and the Sync Now link appears at the top right. Click "Sync Now" and wait for the sync to complete.
- Configure the obfuscation script : Before compiling the APK, you need to configure the obfuscation configuration file to avoid confusing the HMS Core SDK and causing abnormal functions.
Open the obfuscation configuration file "proguard-rules.pro" in the application-level root directory, and add the obfuscation configuration script that excludes the HMS Core SDK.
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.huawei.hianalytics.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}
- Import the certificate fingerprint, modify the package name, and configure the JDK compiled version :
Generate a Keystore file and import it into the application according to the " Creating an Application Keystore File " guide. After importing, open the application-level "build.gradle" file to see the result of the import.
Modify the application package name. The application package name should be consistent with the package name filled in "Apply for Account Service".
Open the "build.gradle" file in the application-level root directory under the App, and add the compileOptions configuration in "android". The configuration format is as follows:
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
Main implementation code
- Pull up the login page for login authorization.
/**
* Add scopes to apply for and obtains the authorization process Intent.
*/
private void requestAuth() {
// Add scopes to apply for. The following only shows an example.
// Developers need to add scopes according to their specific needs.
String[] allScopes = Scopes.getAllScopes();
// Obtains the authorization process Intent.
// True indicates that the health app authorization process is enabled. False indicates disabled.
Intent intent = mSettingController.requestAuthorizationIntent(allScopes, true);
// The authorization process page is displayed.
startActivityForResult(intent, REQUEST_AUTH);
}
- Call the readLatestData() interface of the com.huawei.hms.hihealth.DataController class to read the latest health-related data. Including the user's height, weight, heart rate, blood pressure, blood sugar, and blood oxygen.
/**
* read the latest data basing on data type
*
* @param view (indicating a UI object)
*/
public void readLatestData(View view) {
// 1. Use the specified data type (DT_INSTANTANEOUS_HEIGHT) to call the data controller to query
// the latest data of this data type.
List<DataType> dataTypes = new ArrayList<>();
dataTypes.add(DataType.DT_INSTANTANEOUS_HEIGHT);
dataTypes.add(DataType.DT_INSTANTANEOUS_BODY_WEIGHT);
dataTypes.add(DataType.DT_INSTANTANEOUS_HEART_RATE);
dataTypes.add(DataType.DT_INSTANTANEOUS_STRESS);
dataTypes.add(HealthDataTypes.DT_INSTANTANEOUS_BLOOD_PRESSURE);
dataTypes.add(HealthDataTypes.DT_INSTANTANEOUS_BLOOD_GLUCOSE);
dataTypes.add(HealthDataTypes.DT_INSTANTANEOUS_SPO2);
Task<Map<DataType, SamplePoint>> readLatestDatas = dataController.readLatestData(dataTypes);
// 2. Calling the data controller to query the latest data is an asynchronous operation.
// Therefore, a listener needs to be registered to monitor whether the data query is successful or not.
readLatestDatas.addOnSuccessListener(new OnSuccessListener<Map<DataType, SamplePoint>>() {
@Override
public void onSuccess(Map<DataType, SamplePoint> samplePointMap) {
logger("Success read latest data from HMS core");
if (samplePointMap != null) {
for (DataType dataType : dataTypes) {
if (samplePointMap.containsKey(dataType)) {
showSamplePoint(samplePointMap.get(dataType));
handleData(dataType);
} else {
logger("The DataType " + dataType.getName() + " has no latest data");
}
}
}
}
});
readLatestDatas.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
String errorCode = e.getMessage();
String errorMsg = HiHealthStatusCodes.getStatusCodeMessage(Integer.parseInt(errorCode));
logger(errorCode + ": " + errorMsg);
}
});
}
The DataType object contains the specific data type and the value of the data, and the corresponding data can be obtained by parsing.
Learn more details>>
Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee
Follow us to know the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。