Overview

5G Modem Kit introduces the crowdtesting function, making rich modem parameters accessible. With the crowdtesting API, you can obtain more comprehensive network parameters to support network evaluation and tuning.

What You Will Create

In the codelab, you will create an app integrated with 5G Modem Kit to query modem parameters.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

To integrate 5G Modem Kit, you must complete the following preparations:
  1. Create an Android Studio project.
  2. Add necessary configurations.
  3. Configure the project signature.
For details, please refer to Preparations for Integrating HUAWEI HMS Core.

Configuring the Maven Repository Address for the HMS Core SDK

The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin earlier than 7.0, Gradle plugin 7.0, and Gradle plugin 7.1 or later. Click a relevant link below to find the configuration procedure for the specific Gradle plugin version.

Gradle plugin earlier than 7.0

Gradle plugin 7.0

Gradle plugin 7.1 or later

Adding Build Dependencies

  1. Open the build.gradle file in the app directory of your project.
  2. Add a build dependency in the dependencies block.
    dependencies { implementation 'com.huawei.hms:hms5gmodem-crowdtesting:{version}' }
  3. Configure compilation options for 5G Modem Kit to use Java 8 language features, such as lambda expressions.

    android{ compileOptions{ sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }

Synchronizing the Project

After completing the preceding configuration, click the synchronization icon on the toolbar to synchronize the Gradle files.

Configuring Obfuscation Scripts

Before building the APK, configure the obfuscation configuration file to prevent the HMS Core SDK from being obfuscated.

  1. Open the obfuscation configuration file proguard-rules.pro in the app directory of your project, and 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.huawei.hms5gkit.**{*;}
  2. If you are using AndResGuard, add its trustlist to the build.gradle file in the app directory of your project.
    "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*"

Adding Permissions

  1. Open the AndroidManifest.xml file in your project.
  2. Add permissions required by your app.
    <!–Allow the app to access the Internet.–> <uses-permission android:name="android.permission.INTERNET" /> <!–Allow the app to query the network status.–> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!–Allow the app to query the Wi-Fi status.–> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!–Allow the app to receive location information from satellites through the GPS chip.–> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

In the previous section, you have successfully integrated the 5G Modem SDK.
In this section, you will build a demo app and learn how to use 5G Modem Kit APIs simply by performing the following steps:

Querying Modem Parameters

  1. Obtain a parameter query instance of 5G Modem Kit.
    private IQueryModem mQueryModem = QueryModemController.getInstance();
  2. Use the instance to register 5G Modem SDK callback functions.
    // Register the 5G Modem SDK callback functions: IResProcess used for parameter query and IConnectProcess used for service connection. public boolean registerCallback(Context context) { return mQueryModem.registerCallback(context, mResProcess, mConnectProcess); } // IResProcess is the parameter query callback function. private IResProcess mResProcess = response -> { if (response != null && response.getCode() == 0) { // key indicates a requested parameter. String key = response.getQueryParameters(); // data indicates the returned parameter value. String data = response.getValue(); String content = ""; if (data != null) { content += key + " request result: " + data; } else { content += key + " request result is null"; } Log.i(TAG, content); } else { // A null pointer or a non-zero result code indicates that the requested parameter and its value are invalid. if (response != null) { String content = "error code: " + response.getCode() + ",\t" + response.getMsg(); Log.e(TAG, content); } else { Log.e(TAG, "response is null"); } } }; // IConnectProcess is the service connection callback function. private IConnectProcess mConnectProcess = response -> { if (response == null) { Log.e(TAG, "ConnectProcess callback response data is null"); return; } String content; // A non-zero result code indicates abnormal connection. msg indicates the error message. if (response.getCode() != 0) { content = "connect error code: " + response.getCode() + ", error msg: " + response.getMsg(); Log.e(TAG, content); } else { content = "connect code: " + response.getCode() + ", msg: " + response.getMsg(); Log.i(TAG, content); } };
  3. Use the instance to query modem parameters. For details about the parameters, please refer to Constant-values.
    // Check the callback function registration. // false if the registration fails or your app is disconnected from the low-level service. public boolean getConnectStatus() { return mQueryModem.getAidlConnectStatus(); } // Query the parameters. public boolean queryModem(String requestName) { return mQueryModem.queryModem(requestName); }
  4. (Optional) Unregister the callback functions with the instance if you have no more modem parameters to query.
    // Unregister the callback functions and disconnect your app from the low-level service. public void unRegisterCallback() { mQueryModem.unRegisterCallback(); }

Building, Loading, and Debugging the App

After completing the above service code, you can build the demo. After the demo is built, an APK file will be generated. Install and run it on the test phone.

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

For more information about 5G Modem Kit, please visit our official website. If you encounter any problem during the development, please refer to FAQs.

For more information, please click the following links: API Reference, Sample Code
To download the sample code, please click the button below:

Download

Code copied