[Huawei Developers] muraliameakula:
Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover integration of Huawei Enterprise Manager (HEM) Kit in Android.

Huawei Enterprise Manager (HEM) is a mobile device management solution provided for you based on the powerful platform and hardware of Huawei. The device deployment service in HEM helps install a Device Policy Controller (DPC) app automatically on enterprise devices in batches.

Development Overview

You need to install Android studio IDE and I assume that you have prior knowledge about the Android and java.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

A Huawei phone (with the USB cable), which is used for debugging.

An enterprise-oriented Huawei phone that has not been activated (running EMUI 11.0 or later). The bring your own device (BYOD) mode is not supported

Software Requirements

Java JDK installation package.

Android studio IDE installed.

HMS Core (APK) 5.X or later.

Follows the steps.

1. Create Android Project.

Open Android Studio.

Click NEW Project, select a Project Templet.

Enter project and Package Name and click on Finish:

2. Register as Huawei developer and complete identity verification in Huawei developer’s website, refer to register a Huawei ID.

3. To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > app > Tasks > android, and then click signingReport, as follows.

or

Also we can generate SHA-256 using command prompt.

To generating SHA-256 certificate fingerprint use below command.

4. Create an App in AppGallery Connect.

5. Download the agconnect-services.json file from AGC, copy and paste in android Project under app directory, as follows.

6. Add the below maven URL in build.gradle(Project level) file under the repositories of buildscript, , for more information refer Add Configuration.

maven { url 'https://developer.huawei.com/repo/' }

7. Add the below plugin and dependencies in build.gradle(App level).

apply plugin 'com.huawei.agconnect'
implementation "com.huawei.hms:hemsdk:1.0.0.303"
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

8. Open AndroidManifest file and add below permissions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

9. Development Procedure.

1. Create a java class MainActivity.java inside your package.

MainActivity.java

package com.android.hemdemokit;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.huawei.hem.license.HemLicenseManager;
import com.huawei.hem.license.HemLicenseStatusListener;

public class MainActivity extends Activity {
private HemLicenseManager hemInstance;

private TextView resultCodeTV;

private TextView resultCodeDescTV;

private Button btnActive;

private Button btnDeActive;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hemInstance = HemLicenseManager.getInstance(this);
setButtonClickListener();
setStatusListener();

}

private void setButtonClickListener() {
btnActive = findViewById(R.id.active_btn);
btnDeActive = findViewById(R.id.de_active_btn);
esultCodeTV = findViewById(R.id.result_code_tv);
resultCodeDescTV = findViewById(R.id.result_code_desc_tv);
btnActive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hemInstance.activeLicense();
}
});

btnDeActive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hemInstance.deActiveLicense();
}
});
}

private void setStatusListener() {
hemInstance.setStatusListener(new MyHemLicenseStatusListener());
}

private class MyHemLicenseStatusListener implements HemLicenseStatusListener {
@Override
public void onStatus(final int errorCode, final String msg) {
resultCodeTV.post(new Runnable() {
@Override
public void run() {
resultCodeTV.setText(String.valueOf(errorCode));
}
});

resultCodeDescTV.post(new Runnable() {
@Override
public void run() {
resultCodeDescTV.setText(msg);
}
});
}
}
}

2. Create activity_main.xml layout file under app > main > res > layout folder.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="return code:"
android:textSize="16dp" />

<TextView
android:id="@+id/result_code_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@null"
android:drawablePadding="10dp"
android:padding="10dp"
android:text="" />
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:orientation="horizontal">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="result description:"
android:textSize="16dp" />

<TextView
android:id="@+id/result_code_desc_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@null"
android:drawablePadding="10dp"
android:padding="10dp"
android:text="" />
</LinearLayout>

<Button
android:id="@+id/active_btn"
android:text="call active"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/de_active_btn"
android:text="call de_active"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

10. To build apk and run in device, choose Build > Generate Signed Bundle/APK > Build for apk or Build and Run into connected device follow the steps.

Result

1. Install application into device and click on app icon you can see below result. ​

2. If the EMUI device is less than targeted device, then you will get below errors.

Tips and Tricks

Always use the latest version of the library.

Add agconnect-services.json file without fail.

Add SHA-256 fingerprint without fail.

Make sure dependenciesadded in build files.

Make sure you have EMUI 11.0 and later versions.

Conclusion

In this article, we have learnt integration of Huawei HEM sdk. Also we learnt how to activate and deactivate an MDM license. HEM kit enables you to flexibly adapt your app to a wide range of device deployment scenarios for enterprises, to implement auto-deployment when they enroll a bunch of devices out of the box. This, in turn, dramatically reduces the required manual workload.

References

HEM Kit: https://developer.huawei.com/consumer/en/hms/huawei-hemkit/

Original Source