tencent cloud

IoT Hub

IoT Hub 정책
개인 정보 보호 정책
데이터 개인 정보 보호 및 보안 계약
문서IoT Hub

SDK for Android Project Configuration

포커스 모드
폰트 크기
마지막 업데이트 시간: 2024-12-27 17:03:43
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. You only need to complete the corresponding project configuration to connect devices.

Prerequisites

Products and devices have been created as instructed in Device Connection Preparations.

How to Import

SDK integration

If you don't need to run the IoT Hub SDK in the service component, only dependency on iot_core is required.
Depend on Maven for remote build. Below is the sample code:
dependencies {
implementation 'com.tencent.iot.hub:hub-device-android-core:x.x.x'
implementation 'com.tencent.iot.hub:hub-device-android-service:x.x.x'
}
Note:
You can set the above x.x.x to the latest version according to SDK for Android Release Notes.
If you don't need to run the IoT Hub SDK in the service component, only dependency on iot_core is required.
If you need to run the IoT Hub SDK in the service component, only dependency on iot_service is required.
Depend on the local SDK source code for build: Modify the build.gradle of the application module to make it dependent on the iot_core and iot_service source code. Below is the sample code:
dependencies {
implementation project(':hub:hub-device-android:iot_core')
implementation project(':hub:hub-device-android:iot_service')
}

Connection Authentication

Edit the configuration information in the app-config.json file so that the following data in IoTMqttFragment.java can be read:
{
"PRODUCT_ID": "",
"DEVICE_NAME": "",
"DEVICE_PSK": "",
"SUB_PRODUCT_ID": "",
"SUB_DEV_NAME": "",
"SUB_PRODUCT_KEY": "",
"TEST_TOPIC": "",
"SHADOW_TEST_TOPIC": "",
"PRODUCT_KEY": ""
}
The SDK supports two authentication methods: certificate authentication and key authentication, which should be selected and set according to the authentication type of the created product.
For key authentication, you need to enter the parameters corresponding to PRODUCT_ID, DEVICE_NAME, and DEVICE_PSK in the configuration information in app-config.json. The SDK will automatically generate a signature based on the device configuration information as a credential for connection to IoT Hub.
For certificate authentication, you need to enter the PRODUCT_ID and DEVICE_NAME in the configuration information in app-config.json and read the content of the device certificate and private key files in either of the following two ways:
Read through AssetManager. In this case, you need to create the assets directory under the hub/hub-android-demo/src/main path of the project and place the device certificate and private key files in it.
Read through InputStream. In this case, you need to pass in the full path information of the device certificate and private key files.
1.1 After successfully reading the certificate and private key files, you need to set the mDevCertName certificate name and mDevKeyName private key name in IoTMqttFragment.java.
private String mDevCertName = "YOUR_DEVICE_NAME_cert.crt";
private String mDevKeyName = "YOUR_DEVICE_NAME_private.key";
1.2 After the configuration is completed, call the MQTT connection APIs of the SDK in the project to complete device connection.
mMqttConnection = new TXGatewayConnection(mContext, mBrokerURL, mProductID, mDevName, mDevPSK,null,null ,mMqttLogFlag, mMqttLogCallBack, mMqttActionCallBack);
mMqttConnection.connect(options, mqttRequest);

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백