The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

SDK for Android Project Configuration

Last updated: 2025-03-19 15:03:07

IoT Hub device SDK for Android relies on a secure and powerful data channel to enable IoT developers to quickly connect devices to the cloud for two-way communication. Developers only need to complete the corresponding project configuration to connect devices.

Prerequisites

The product and device have been created according to the Device Access Preparation.

Reference Method

Integrated SDKs

Dependencies: Maven Remote Construction, as shown below:
dependencies {
implementation 'com.tencent.iot.hub:hub-device-android:x.x.x'
}
Note:
You can set the above x.x.x to the latest version according to Version Explanation .
Depend on local SDK source code to build: Modify the application's build.gradle so that the application module depends on the source code. The example is as follows:
dependencies {
implementation project(':hub:hub-device-android')
}

Connection Authentication

Edit the configuration information in the app-config.json file, you can read the corresponding data in IoTMqttFragment.java:
{
"PRODUCT_ID": "",
"DEVICE_NAME": "",
"DEVICE_PSK": "",
"SUB_PRODUCT_ID": "",
"SUB_DEV_NAME": "",
"SUB_PRODUCT_KEY": "",
"TEST_TOPIC": "",
"SHADOW_TEST_TOPIC": "",
"PRODUCT_KEY": ""
}
The SDK provides two authentication methods: Certificate Authentication and Key Authentication. You need to select and configure according to the authentication type of the created product.
For Key Authentication, fill in the parameters PRODUCT_ID, DEVICE_NAME, and DEVICE_PSK in the app-config.json configuration. The SDK will automatically generate a signature based on the device configuration information to serve as a credential for accessing the IoT Hub.
For Certificate Authentication, fill in the PRODUCT_ID, DEVICE_NAME, etc., in the app-config.json configuration and read the contents of the device certificate and device private key files. There are two ways to read:
Read through AssetManager, at this time, you need to create an assets directory under the path hub/hub-android-demo/src/main and place the device certificate and private key in that directory.
Read through InputStream, at this time, you need to pass in the full path information of the device certificate and private key.
1.1 After successfully reading the certificate file and private key file, you need to set the certificate name mDevCertName and private key name mDevKeyName in IoTMqttFragment.java.
private String mDevCertName = "YOUR_DEVICE_NAME_cert.crt";
private String mDevKeyName = "YOUR_DEVICE_NAME_private.key";
1.2 After configuration is completed, you can call the relevant interfaces of MQTT connection in the SDK in the project to complete the device connection.
mMqttConnection = new TXGatewayConnection(mContext, mBrokerURL, mProductID, mDevName, mDevPSK,null,null ,mMqttLogFlag, mMqttLogCallBack, mMqttActionCallBack);
mMqttConnection.connect(options, mqttRequest);