[Huawei Developers] Freemind R:
More articles like this, you can visit HUAWEI Developer Forum​

Want to retrieve health or fitness data of users based on their HUAWEI ID and show in your app?

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/083/299/2640060000001083299.20200716090723.00199462312605416561084272055233:50510723090815:2800:5F2BFEE8A32B7825EEEF123EDBEB3BCE5F50406FD757060D27D969B81DA7E61F.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/083/299/2640060000001083299.20200716090723.00199462312605416561084272055233:50510723090815:2800:5F2BFEE8A32B7825EEEF123EDBEB3BCE5F50406FD757060D27D969B81DA7E61F.jpg[/IMG]
​

Health Kit provide main three feature:

Data storage

Provides a data platform for developers to store fitness and health data.

Data openness

Provides a wide range of fitness and health APIs and supports sharing of the various fitness and health data, including step count, weight, and heart rate.

Data access authorization management

Provides settings for users and developers so users can manage developer's access to their health and fitness data, guaranteeing users' data privacy and legal rights.

Developement

1.Register HUAWEI ID and apply for Account Service*

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/apply-id-0000001050069756

2. Applying for Health Kit in developer console*

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/apply-kitservice-0000001050071707

3. Integrate HMS Health Kit

In Build.gradle file,add the implementation.

implementation 'com.huawei.hms:hihealth-base:{5.0.0.300}

In AndroidManifest.xml file, add the app ID generated when the creating the app on HUAWEI Developers to the application section.

<meta-data
android:name="com.huawei.hms.client.appid"
android:value="APP ID"/>

4.Call the related APIs to display HUAWEI ID sign-in screen and authorization screen. The app can only access data upon user authorization.

Sign in via HMS Core SDK and apply for the scope to obtain the permissions to access the HUAWEI Health Kit APIs.

private void signInn() {
Log.i(TAG, "begin sign in");
List<Scope> scopeList = new ArrayList<>();

// Add scopes to apply for. The following only shows an example.
// Developers need to add scopes according to their specific needs.

// View and save steps in HUAWEI Health Kit.
scopeList.add(new Scope( Scopes.HEALTHKIT_STEP_BOTH));

// View and save height and weight in HUAWEI Health Kit.
scopeList.add(new Scope(Scopes.HEALTHKIT_CALORIES_BOTH));

// View and save the heart rate data in HUAWEI Health Kit.
scopeList.add(new Scope(Scopes.HEALTHKIT_DISTANCE_BOTH));

// Configure authorization parameters.
HuaweiIdAuthParamsHelper authParamsHelper =
new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM);
HuaweiIdAuthParams authParams =
authParamsHelper.setIdToken().setAccessToken().setScopeList(scopeList).createParams();

// Initialize the HuaweiIdAuthService object.
final HuaweiIdAuthService authService =
HuaweiIdAuthManager.getService(this.getContext(), authParams);

// Silent sign-in. If authorization has been granted by the current account,
// the authorization screen will not display. This is an asynchronous method.
Task<AuthHuaweiId> authHuaweiIdTask = authService.silentSignIn();

final Context context = this.getContext();

// Add the callback for the call result.
authHuaweiIdTask.addOnSuccessListener(new OnSuccessListener<AuthHuaweiId>() {
@Override
public void onSuccess(AuthHuaweiId huaweiId) {
// The silent sign-in is successful.
Log.i(TAG, "silentSignIn success");

try {
readData();
} catch (ParseException e) {
e.printStackTrace();
}
Toast.makeText(context, "silentSignIn success", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception exception) {
// The silent sign-in fails.
// This indicates that the authorization has not been granted by the current account.
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.i(TAG, "sign failed status:" + apiException.getStatusCode());
Log.i(TAG, "begin sign in by intent");

// Call the sign-in API using the getSignInIntent() method.
Intent signInIntent = authService.getSignInIntent();

MeFragment.this.startActivityForResult(signInIntent, REQUEST_SIGN_IN_LOGIN);
}
}
});
}

5.Build the condition for data query: a DataCollector object

DataCollector dataCollector = new DataCollector.Builder().setPackageName(context)
.setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
.setDataStreamName("STEPS_DELTA")
.setDataGenerateType(DataCollector.DATA_TYPE_RAW)
.build();

6.Build time range and build condition-based query object.

Use the specified condition query object(call read Option) to call the data controller to query the sampling dataset.

public void readData() throws ParseException {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date startDate = dateFormat.parse("2020-03-17 09:00:00");
Date endDate = dateFormat.parse("2020-07-17 09:05:00");
ReadOptions readOptions = new ReadOptions.Builder()
.read(DataType.DT_CONTINUOUS_STEPS_DELTA)
.read(DataType.DT_CONTINUOUS_CALORIES_BURNT)
.read(DataType.DT_CONTINUOUS_DISTANCE_DELTA)
.setTimeRange(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
.build();
Task<ReadReply> readReplyTask = dataController.read(readOptions);
readReplyTask.addOnSuccessListener(new OnSuccessListener<ReadReply>() {
@Override
public void onSuccess(ReadReply readReply) {
for (SampleSet sampleSet : readReply.getSampleSets()) {
showSampleSet(sampleSet);
Log.i(TAG,"*****************"+sampleSet);
}

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
checkData.setText(e.toString()+"read");
}
});
}

7.the calories,distance and step are succesfully show in app.

Reference:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/overview-dev-android-0000001050071671