Android Link SDK
Connect a simulated device to IoT Platform over MQTT using the Android Link SDK in a Windows environment, and report data through a custom topic. This example uses a night light device.
Prerequisites
The IoT Platform service has been activated.
You are not charged for activating IoT Platform. You can try the service for free. For more information, see Alibaba Cloud Free Tier for IoT Platform. For information about billing, see Billing overview.
Create a product and a device
-
Log on to the IoT Platform console and click the card for your target instance.
-
In the left-side navigation pane, choose Device Management > Products, and then click Create Product.
-
On the Create Product page, set Product Name to
Night Light, select Custom Category, keep the defaults for other parameters, and click OK. -
In the Add Device section, click Add.
-
In the Add Device dialog box, enter a device name, such as
LightSwitch, and then click OK. -
In the The devices have been added. dialog box, click Copy Device Certificate. Save the device credentials locally for later use.
Device credentials include
productKey,deviceName, anddeviceSecret, required to connect to IoT Platform. device certificates. -
On the product page, click View.
-
On the product details page, choose Topic Categories > Custom Topics > Define Topic Category to create custom topics.
Create one topic with Subscribe permissions and another with Publish permissions. Save the displayed topic strings for later use.
-
Subscribe:
/a18wP**/${deviceName}/user/get -
Publish:
/a18wP**/${deviceName}/user/updata
-
-
Click OK.
Prepare the development environment
This example uses Android Studio 4.1.1 and a virtual device running Android 10.0, as described in Environment description.
-
Install Android Studio.
Download from Android Studio.
-
Create a virtual device.
Configure the device SDK
-
Download the demo package to your development environment and extract the files.
NoteBy downloading this demo, you agree to the software license agreement.
-
Open Android Studio, choose File > Open..., locate the downloaded demo files, and then configure the parameters in the following files.
File
Parameter
Example
Description
app/src/main/res/raw/deviceinfoproductKey
a18wP******
Device credentials. Replace the placeholders with the credentials you saved earlier.
deviceName
LightSwitch
deviceSecret
uwMTmVAMnGGHaAkqmeDY6cHxxB******
app/src/main/java/com/aliyun/alink/devicesdk/manager/InitManager.javaLinkKit.getInstance().init
MqttConfigure.mqttHost = "iot-060a****.mqtt.iothub.aliyuncs.com:8883"
The device endpoint.
Before
LinkKit.getInstance().init, add the following code:MqttConfigure.mqttHost = "YourHostName:8883";Replace
YourHostNamewith the endpoint for your instance. You can obtain the endpoint in one of the following ways:-
Enterprise Edition or new public instance: On the instance details page, click View Development Configurations. Under MQTT device access, click Copy to copy the endpoint.
-
Legacy public instance: Construct the endpoint in the following format:
${productKey}.iot-as-mqtt.${YourRegionId}.aliyuncs.com:8883Example:
a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com:8883
-
-
Click the
button to compile the project.
Verify results
-
After the project is compiled, click the
button to run it.The demo app connects to IoT Platform on the virtual device. The home page lists feature examples, including Mqtt Example.
-
Click Mqtt Example.
-
On the Mqtt Example page, enter the subscribe topic you saved earlier in Subscribe topic, and click Subscribe.
NoteReplace the placeholders in the topic string (for example,
/a18wP******/${deviceName}/user/get) with your actualproductKeyanddeviceName.After subscribing, the device receives messages from IoT Platform. Send a test message to verify the connection.
In the IoT Platform console, go to the Device Details page and open the Topic List tab. Find the subscribed topic and click Publish Message. In the Publish Message dialog box, enter a message such as
This is a test message from Alibaba IoT Platform. -
On the Mqtt Example page, enter the publish topic you saved earlier in Publish message topic, type a message, and click Publish.
For example, set the payload to
{"id":"1","version":"1.0","params":{"state":"1"}}and select 0 for Qos Level.On the Device Log page, click View to confirm the device message reached IoT Platform.
-
Next steps
After connecting your device, manage, monitor, and maintain it through IoT Platform. Features.
Configure advanced features through the Android Link SDK documentation.