Awareness Kit and Health Kit are most common services in health-related apps. Awareness Kit is used to detect user behavior activities and Health Kit is used to read the step count.
This codelab will use Awareness Kit and Health Kit to implement the use case below.

This codelab describes the integration of multiple kits like Awareness Kit and Health Kit. Moreover, Awareness Kit helps the app quickly obtain the user behavior activities and Health Kit helps the app read user's real-time data, such as step count.

Service Scenario

This codelab will be helpful for the apps that are going to be developed under health, sports and medical domains. Awareness Kit is used to detect the user behavior activities and Health Kit is used to constantly synchronize the step count and other physical activity details. Also, this project will calculate user calories burned information automatically based on the data provided by Health Kit.

What You Will Create

In this codelab, you will create a demo project, and use Awareness Kit and Health Kit to read the user step count in real time and calculate calories burned data.

App flow diagram

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate HMS Core kits, you need to complete the following preparations:

Check User Behavior Status Using Awareness Kit

To obtain behavior change of a user, you will need to use the HMS Core Awareness SDK, and your app must initialize the SDK when starting up. Register the receiver class and call add barrier code in the onCreate() method of the class.

Java:

// receiver class initialization private void registerReceiverClass() { final String barrierReceiverAction = getActivity().getApplication().getPackageName() + "BEHAVIOR_BARRIER_RECEIVER_ACTION"; Intent intent = new Intent(barrierReceiverAction); // You can also create PendingIntent with getActivity() or getService(). // This depends on what action you want Awareness Kit to trigger when the barrier status changes. mPendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Register a broadcast receiver to receive the broadcast sent by Awareness Kit when the barrier status changes. mBarrierReceiver = new BehaviorBarrierReceiver(); getActivity().registerReceiver(mBarrierReceiver, new IntentFilter(barrierReceiverAction)); AwarenessBarrier beginWalkingBarrier = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { beginWalkingBarrier = BehaviorBarrier.beginning(BehaviorBarrier.BEHAVIOR_WALKING); } AddBarrierForAwarness.addBarrier(getActivity(), getResources().getString(R.string.BEGINNING_BARRIER_LABEL), beginWalkingBarrier, mPendingIntent); }

Kotlin:

// receiver class initialization private fun registerReceiverClass() { val barrierReceiverAction: String = (activity?.getApplication()?.getPackageName() ?: "") + resources.getString(R.string.BEHAVIOR_ACTION) val intent = Intent(barrierReceiverAction) // You can also create PendingIntent with getActivity() or getService(). // This depends on what action you want Awareness Kit to trigger when the barrier status changes. mPendingIntent = PendingIntent.getBroadcast(activity, DefaultValues.DEFAULT_VALUE, intent, PendingIntent.FLAG_UPDATE_CURRENT) // Register a broadcast receiver to receive the broadcast sent by Awareness Kit when the barrier status changes. mBarrierReceiver = BehaviorBarrierReceiver() activity?.registerReceiver(mBarrierReceiver, IntentFilter(barrierReceiverAction)) var beginWalkingBarrier: AwarenessBarrier? = null if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { beginWalkingBarrier = BehaviorBarrier.beginning(BehaviorBarrier.BEHAVIOR_WALKING) } // Add the beginning barrier to listen user behavior changes. activity?.let { AddBarrierForAwarness.addBarrier(it, resources.getString(R.string.BEGINNING_BARRIER_LABEL), beginWalkingBarrier, mPendingIntent) } }

Step 1 : Use the add barrier code to obtain the behavior status

Java:

/* * Add the barrier and get the behavior changes * * @param Context application context * @param string label used to identify barrier * @param AwarenessBarrier barrier to check kind of request */ public static void addBarrier(Context context, final String label, AwarenessBarrier barrier, PendingIntent pendingIntent) { BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder(); // When the status of the registered barrier changes, pendingIntent is triggered. // Label is used to uniquely identify the barrier. You can query a barrier by the label and delete it. BarrierUpdateRequest request = builder.addBarrier(label, barrier, pendingIntent).build(); Awareness.getBarrierClient(context).updateBarriers(request) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { showToast(context, context.getString(R.string.add_barrier_success)); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { showToast(context, e.toString()); } }); }

Kotlin:

/* * Add the barrier and get the behavior changes * * @param Context application context * @param string label used to identify barrier * @param AwarenessBarrier barrier to check kind of request */ fun addBarrier(context: Context, label: String?, barrier: AwarenessBarrier?, pendingIntent: PendingIntent?) { val builder = BarrierUpdateRequest.Builder() // When the status of the registered barrier changes, pendingIntent is triggered. // Label is used to uniquely identify the barrier. You can query a barrier by the label and delete it. val request = builder.addBarrier(label!!, barrier!!, pendingIntent!!).build() Awareness.getBarrierClient(context).updateBarriers(request) .addOnSuccessListener { showToast(context, "add barrier success") } .addOnFailureListener { e - showToast(context, e.toString())}}

Step 2: Use the receiver class code to listen the event of behavior changes

Java:

// Broadcast receiver to listen the event final class BehaviorBarrierReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BarrierStatus barrierStatus = BarrierStatus.extract(intent); String label = barrierStatus.getBarrierLabel(); int barrierPresentStatus = barrierStatus.getPresentStatus(); switch (label) { case BEGINNING_BARRIER_LABEL : if (barrierPresentStatus == BarrierStatus.TRUE) { binding.tvStatus.setText(getResources().getText(R.string.user_status_start_walking)); binding.tvStatus.setText(getResources().getText(R.string.user_status_walking)); CommonUtil.logger(getResources().getString(R.string.startwalk),TAG); // The user starts to walk. callToStartStepCount(); } break; case ENDING_BARRIER_LABEL : binding.tvStatus.setText(getActivity().getText(R.string.user_status_stop_walking)); CommonUtil.logger(getResources().getString(R.string.stopwalking),TAG); // The user stops walking. callToStopStepCount(); break; default: binding.tvStatus.setText(getActivity().getText(R.string.user_status)); break; } } }

Kotlin:

// Broadcast receiver to listen the event inner class BehaviorBarrierReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val barrierStatus = BarrierStatus.extract(intent) val label = barrierStatus.barrierLabel val barrierPresentStatus = barrierStatus.presentStatus when (label) { BEGINNING_BARRIER_LABEL -> if (barrierPresentStatus == BarrierStatus.TRUE) { // The user starts to walk so call the method to start to read step count. callToStartStepCount() } ENDING_BARRIER_LABEL -> if (barrierPresentStatus == BarrierStatus.TRUE) { // The user stops walking so call the method to stop reading step count. callToStopStepCount() } } } }

Read Real-time Step Count Using Health Kit

Step 1: AGC Console

  1. Select Health Kit.
  2. Click Apply for Health Kit.
  3. Select the required data to obtain and click Submit.
  4. Check that the app is enabled.

Step 2: Initialize Health Kit in onCreate of the Activity

Java:

// Initialize and create an instance of Health Kit in oncreate of activity. HiHealthOptions options = HiHealthOptions.builder().build(); AuthHuaweiId signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(options); autoRecorderController = HuaweiHiHealth.getAutoRecorderController(getActivity(), signInHuaweiId);

Kotlin:

// Initialize and create an instance of Health Kit in oncreate of activity. val options = HiHealthOptions.builder().build() val signInHuaweiId = HuaweiIdAuthManager.getExtendedAuthResult(options) mSettingController = activity?.let { HuaweiHiHealth.getSettingController(it, signInHuaweiId) } controller = activity?.let { HuaweiHiHealth.getAutoRecorderController(it, signInHuaweiId) }

Step 3: Authorization of Health Kit
Follow the following three steps to complete the Health app authorization.

Well done. You have successfully built a My Health app and learned how to:

  1. Awareness Kit
  2. Health Kit

You can also click the button below to download the source code.
Download Source Code

Code copied