tencent cloud

IoT Hub

Release Notes and Announcements
Release Notes
Product Introduction
Overview
Features
Strengths
Use Cases
Use Limits
Basic Concepts
Getting Started
Quick Start
Scenario 1: Device Interconnection
Scenario 2: Device Status Reporting and Setting
MQTT.fx Connection Guide
Console Guide
Product Management
Rule Engine
Sub-account Access to IoT Hub
Firmware Upgrade
Resource Management
Certificate Management
Developer Manual
Feature Components
Signature Algorithm
Device Authentication
Device Connection Protocol
Gateway Subdevice
Message Communication
Device Shadow
Device Firmware Upgrade
Remote Device Configuration
Resource Management
Device Log Reporting
NTP Service
Device Connection Manual
Device Connection Overview
Connection Based on SDK for C
Connection Based on SDK for Android
Connection Based on SDK for Java
Connection Based on SDK for Python
API Documentation
History
Introduction
API Category
Making API Requests
Device Shadow APIs
Device APIs
CA Certificate APIs
Product APIs
Data Types
Error Codes
FAQs
General
Device Connection and Reporting
Rule Engine
Console
IoT Hub Policy
Privacy Policy
Data Privacy And Security Agreement
Glossary

SDK for Android Project Configuration

PDF
Focus Mode
Font Size
Last updated: 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);

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback