Subscribe to daily Health Data using Huawei Health

Anil Ghimire
2 min readMay 10, 2022

Huawei Health kit connects the hardware devices and ecosystem apps providing users with health care, workout guidance, and ultimate service experience and developers a fitness and health data open capabilities so that they can build apps based on the data types.

Currently, you can subscribe to step count, calories burned, and distance data.

Subscription can be done in:

  • Target mode: In this case, the notification is triggered when the user completes a certain target. For Eg: if your app registers for the target of 300 calories burned, your app will receive a notification when the user burns 300 calories of energy.
  • Interval mode: In this case, the notification is triggered every time the specified target is met. For Eg: If your app registers for the interval of 300 calories then the app will receive when the user burns 300, 600, 900…. calories.

In this article, I will explain subscribing and unsubscribing the daily health data generated by Huawei Health. I will subscribe to a target of 300 calories burned, where the app will get a notification when the target is met.

In order to work with the Huawei Health, you have to perform the following tasks:

After the above process, you can start developing the app in Android Studio. Do not forget to add HMS Core to your project and Configuring the Obfuscation Script.

For setting the interval target you do not have to ask permission from the user to access the health data but if you want to set the interval target you have to ask permission from the user. The table below will provide a clear idea.

DataReportModel modelCaloriesInterval = new DataReportModel(HiHealthPointType.DATA_POINT_CALORIES_SUM,
CharacteristicConstant.ReportType.VALUE_INTERVAL.getReportTypeValue(), 300);

// Result callback.
HiRealTimeCallback hiRealTimeCallback = new HiRealTimeCallback() {
@Override
public void onResult(int errCode, String message) {
if (errCode == HiHealthError.SUCCESS) {
Log.i(TAG, “success”);
}
}

@Override
public void onDataChanged(Bundle bundle) {
// Notify the subscriber when the total calories burned reach 30 kcal (the actual calories burned will be returned).
Log.i(TAG, String.valueOf(bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_CALORIE)));
}
};

// Subscribe to calories.
HiHealthDataStore.registerDataAutoReport(context, modelCaloriesInterval, hiRealTimeCallback);

// Cancel the subscription.
HiHealthDataStore.unregisterDataAutoReport(context, modelCaloriesInterval, hiRealTimeCallback);

In the above code snippet, DataReportModel is the daily data subscription model and HiRealTimeCallback is the result callback which provides the actual calories burned value.

registerDataAutoReport method of HiHealthDataStore to subscribe to the daily data. unregisterDataAutoReport method of HiHealthDataStore to unsubscribe from daily data.

--

--