Android Link SDK

Updated at:

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.

Note

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

  1. Log on to the IoT Platform console and click the card for your target instance.

  2. In the left-side navigation pane, choose Device Management > Products, and then click Create Product.

  3. On the Create Product page, set Product Name to Night Light, select Custom Category, keep the defaults for other parameters, and click OK.

  4. In the Add Device section, click Add.

  5. In the Add Device dialog box, enter a device name, such as LightSwitch, and then click OK.

  6. 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, and deviceSecret, required to connect to IoT Platform. device certificates.

  7. On the product page, click View.

  8. 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

  9. 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.

  1. Install Android Studio.

    Download from Android Studio.

  2. Create a virtual device.

    Create and manage virtual devices.

Configure the device SDK

  1. Download the demo package to your development environment and extract the files.

    Note

    By downloading this demo, you agree to the software license agreement.

    Android Link SDK.

  2. 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/deviceinfo

    productKey

    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.java

    LinkKit.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 YourHostName with 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:8883

      Example:

      a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com:8883

  3. Click the Make Project button to compile the project.

Verify results

  • After the project is compiled, click the Run button to run it.

    The demo app connects to IoT Platform on the virtual device. The home page lists feature examples, including Mqtt Example.

    1. Click Mqtt Example.

    2. On the Mqtt Example page, enter the subscribe topic you saved earlier in Subscribe topic, and click Subscribe.

      Note

      Replace the placeholders in the topic string (for example, /a18wP******/${deviceName}/user/get) with your actual productKey and deviceName.

      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.

    3. 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.