HUAWEI Awareness Kit provides your app the ability to obtain contextual information including user's current time, location, behavior, headset status, and nearby beacons. Your app can gain insight into the user's current situation more efficiently, making it possible to deliver a smarter, more considerate user experience.

HUAWEI Awareness Kit offers the Capture API and Barrier API.

What You Will Create

In this codelab, you will create an app that calls HUAWEI Awareness Kit.

What You Will Learn

Hardware Requirements

Software Requirements

HUAWEI HMS Core integration requires the following preparations

For details, see the HUAWEI HMS Core Integration Preparation.

If you are using Android Studio, you can integrate the HMS Core SDK via the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio project.

Add the AppGallery Connect configuration file of the current app to your Android Studio project.

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your app project and click the app that needs to integrate the HMS Core SDK.
  3. Go to Project Setting > General information. In the App information area, download the agconnect-services.json file.
  4. Copy the agconnect-services.json file to the app's root directory of your Android Studio project.

Configure the Maven repository address for the HMS Core SDK.

1. Open the build.gradle file in the root directory of your Android Studio project.

2. Add the AppGallery Connect plug-in and the Maven repository.

Add build dependencies.

  1. Open the build.gradle file in the root directory of your Android Studio project.
  2. Add the following configuration to the build.gradle file:
    compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 }
  3. Configure build dependencies under dependencies and replace {version} with the actual SDK version number, for example, implementation 'com.huawei.hms:awareness:'.
    dependencies{ implementation 'com.huawei.hms:awareness:{version}' }
  4. Add the following information under apply plugin: 'com.android.application' in the file header:
    apply plugin: 'com.huawei.agconnect'
  5. After completing the preceding configuration, click the synchronization icon on the toolbar to synchronize the build.gradle file.

Before building the APK, configure the obfuscation configuration file to prevent the HMS Core SDK from being obfuscated.
The obfuscation configuration file is proguard-rules.pro for Android Studio.

  1. Open the obfuscation configuration file of your Android project.
  2. Add configurations to exclude the HMS Core SDK from obfuscation.
    -ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}
  3. If you have used AndResGuard, add it to the allowlist in the obfuscation configuration file.
    "R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*", "R.string.agc*"

Assigning Permissions in the Manifest File

Before calling awareness capabilities, you need to assign required permissions in the manifest file. The permissions vary with awareness capabilities. For details, see the Development Guide.
For example, if you want to use the headset status awareness capability, enable Bluetooth on the device and assign the Bluetooth permission in the manifest file.

<!--Bluetooth permission --> <uses-permission android:name="android.permission.BLUETOOTH" />

Importing API Classes

You need to import appropriate capability API classes for calling different awareness capabilities. The class paths and names vary with capabilities. For details, see the API Reference.
For example, if you want to use the headset status awareness capability, import the public capability class of the awareness service and the headset status class.

import com.huawei.hmf.tasks.OnFailureListener; import com.huawei.hmf.tasks.OnSuccessListener; import com.huawei.hms.kit.awareness.Awareness; // Import the headset capture-related classes. import com.huawei.hms.kit.awareness.capture.HeadsetStatusResponse; import com.huawei.hms.kit.awareness.status.HeadsetStatus; // Import the headset barrier-related classes. import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier; import com.huawei.hms.kit.awareness.barrier.BarrierStatus; import com.huawei.hms.kit.awareness.barrier.HeadsetBarrier; import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;

Developing Capture Capabilities

  1. Obtain the Capture Client object of Awareness Kit.
  2. Call the headset status query capability API through the Capture Client object to obtain the user's context.
  3. Enable your app to listen for the result returned by Awareness Kit for further processing.
    Awareness.getCaptureClient(this) .getHeadsetStatus() .addOnSuccessListener(new OnSuccessListener<HeadsetStatusResponse>() { @Override public void onSuccess(HeadsetStatusResponse headsetStatusResponse) { HeadsetStatus headsetStatus = headsetStatusResponse.getHeadsetStatus(); int status = headsetStatus.getStatus(); String stateStr = "Headsets are " + (status == HeadsetStatus.CONNECTED ? "connected" : "disconnected"); Log.i(TAG, stateStr); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Log.e(TAG, "get Headsets Capture failed", e); } });

Developing Barrier Capabilities

The following example illustrates how to develop a barrier triggered by the connecting condition. That is, the barrier will be triggered when the headset is connected.

  1. Define the barrier.
    AwarenessBarrier headsetBarrier = HeadsetBarrier.connecting();
  2. Define PendingIntent that will be triggered upon a barrier status change, for example, to send a broadcast, and create a broadcast receiver to receive the broadcast.
    final String BARRIER_RECEIVER_ACTION = getApplication().getPackageName() + "HEADSET_BARRIER_RECEIVER_ACTION"; Intent intent = new Intent(BARRIER_RECEIVER_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); HeadsetBarrierReceiver barrierReceiver = new HeadsetBarrierReceiver(); registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));
  3. Define the label for the barrier and add the barrier.
    String headsetBarrierLabel = "headset connecting barrier"; BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder(); BarrierUpdateRequest request = builder.addBarrier(headsetBarrierLabel, headsetBarrier,pendingIntent).build(); Awareness.getBarrierClient(context).updateBarriers(request) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Toast.makeText(getApplicationContext(), "add barrier success", Toast.LENGTH_SHORT).show(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { Toast.makeText(getApplicationContext(), "add barrier failed", Toast.LENGTH_SHORT).show(); Log.e(TAG, "add barrier failed", e); } });
  4. Define the broadcast receiver to listen for the barrier event for further processing.
    class HeadsetBarrierReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BarrierStatus barrierStatus = BarrierStatus.extract(intent); String label = barrierStatus.getBarrierLabel(); switch(barrierStatus.getPresentStatus()) { case BarrierStatus.TRUE: Log.i(TAG, label + " status:true"); break; case BarrierStatus.FALSE: Log.i(TAG, label + " status:false"); break; case BarrierStatus.UNKNOWN: Log.i(TAG, label + " status:unknown"); break; } } }

After you integrate the SDK, compare the log with the actual status to determine whether an API is called successfully. For example, if the log is "Headsets are unplugged" when you tap get Headset Status with no headsets plugged, the Capture API has been called successfully.

Well done. You have successfully completed this codelab and learned:

For more information about HUAWEI Awareness Kit, please visit our official website. If you encounter any problem during the development, see the FAQs.

For more information, please click the following links:

You can click the button below to download the source code.

Download source code

Code copied