All Products
Search
Document Center

IoT Platform:Communication by using MQTT topics

Last Updated:Jan 24, 2024

Link SDK for Android provides basic API operations that you can call to communicate with IoT Platform by using Message Queuing Telemetry Transport (MQTT) topics. This topic describes how to call API operations to publish messages to, subscribe to, and unsubscribe from MQTT topics.

For information about the sample code, see the MqttActivity.java file in the demo package.

Publish messages

For information about how to publish messages, see Class MqttPublishRequest.

  • Return no response

    The following sample code is used when a device sends a message to IoT Platform and IoT Platform does not respond, or when IoT Platform responds but the device does not need to process the response.

    // Publish a message.
    MqttPublishRequest request = new MqttPublishRequest();
    // Specify whether a response is required. 
    request.isRPC = false;
    // Specify a topic that is used when the device sends messages to IoT Platform. 
    request.topic = "/a18wP******/LightSwitch/user/update";
    // Specify the Quality of Service (QoS) level.
    request.qos = 0;
    String data = "hello world"; // Specify the data that you want the device to include in the message to be published.
    request.payloadObj = data;
    LinkKit.getInstance().publish(request, new IConnectSendListener() {
        @Override
        public void onResponse(ARequest aRequest, AResponse aResponse) {
            // The message is submitted to the sending buffer of the operating system. 
            // IoT Platform may fail to receive the message when network conditions are poor. 
            // If a downstream response is received for the upstream message, we recommend that you check whether the upstream message is received by IoT Platform. 
        }
        @Override
        public void onFailure(ARequest aRequest, AError aError) {
            // The message failed to be published.
        }
    });                       

    Parameter

    Example

    Description

    isRPC

    false

    Specifies whether the message is a Remote Procedure Call (RPC) request. If the message is an RPC request, the response is returned only after a message is received from the topic that is specified by the replyTopic parameter.

    Default value: false. The message does not require responses.

    topic

    /a18wP******/LightSwitch/user/update

    The topic on which you have the Publish permission. The device sends messages to IoT Platform by using this topic.

    qos

    0

    The QoS level in the MQTT request. Default value: 0.

    payloadObj

    {"id":"160865432","method":"thing.event.property.post","params":{"LightSwitch":1},"version":"1.0"}

    The data that you want the device to publish. The data can be in an arbitrary format. If the data is a JSON string, the value of the id field must be unique. You must specify the ID as an auto-increment value. In this example, if the value of the id field in the message is 160865432, the value of the id field in the following message is 160865433.

  • Return a response

    • The following sample code is used when you want to forward the upstream messages of a device to your business server and you want the business server to send downstream messages to the device.

    • The following sample code is also used when you use a topic whose data format conforms to the Alink protocol and the protocol specifies that IoT Platform must return a response.

    // Publish a message.
    MqttPublishRequest request = new MqttPublishRequest();
    // Specify whether a response is required. If you set this parameter to true, a response from IoT Platform is required. 
    request.isRPC = true;
    // Specify the QoS level.
    request.qos = 0;
    // Specify a topic that can be used when the device sends messages to IoT Platform. 
    request.topic = "/a18wP******/LightSwitch/user/update";
    // Specify a topic that can be used when IoT Platform returns responses. If you do not configure this parameter, "topic_reply" is used. 
    request.replyTopic = "/a18wP******/LightSwitch/user/update_reply";
    String data = "hello world"; // Specify the data that you want the device to include in the message to be published.
    request.payloadObj = data;
    // The BaseTemplateActivity class provides sample message responses. onResponse provides the messages that are sent from IoT Platform to devices.
    LinkKit.getInstance().publish(request, new IConnectSendListener() {
        @Override
        public void onResponse(ARequest aRequest, AResponse aResponse) {
            // The message is published.
        }
        @Override
        public void onFailure(ARequest aRequest, AError aError) {
            // The message failed to be published.
        }  
    });                    

    Parameter

    Example

    Description

    isRPC

    true

    Specifies whether the message is an RPC request. If the message is an RPC request, the response is returned only after a message is received from the topic that is specified by the replyTopic parameter.

    If you set this parameter to true, a response from IoT Platform is required.

    qos

    0

    The QoS value in the MQTT request. Default value: 0.

    topic

    /a18wP******/LightSwitch/user/update

    The topic on which you have the Publish permission. The device sends messages to IoT Platform by using this topic.

    replyTopic

    /a18wP******/LightSwitch/user/update_reply

    The topic that can be used when IoT Platform returns responses. If you do not configure this parameter, "topic_reply" is used.

    payloadObj

    {"id":"160865432","method":"thing.event.property.post","params":{"LightSwitch":1},"version":"1.0"}

    The data that you want the device to publish. The data must be a JSON String, the value of the id field must be unique. You must specify the ID as an auto-increment value. In this example, if the value of the id field in the message is 160865432, the value of the id field in the following message is 160865433.

Note

By default, a UI thread returns a response to a message that is sent by the onResponse or onFailure callback of a device. In lp-iot-linkkit V1.7.3 and later, you can specify PersistentConnect.mNotifySendResultOnMainThread = false; to return a response to the message by using a non-UI thread. If UI threads are busy, we recommend that you specify PersistentConnect.mNotifySendResultOnMainThread = false;.

Subscribe to messages

A device can subscribe to messages from a specific topic. The subscription is saved in IoT Platform. After IoT Platform receives a message that is published by a specific topic, IoT Platform forwards the message to the device. For more information, see MqttSubscribeRequest.

After the device subscribes to the messages of the topic, the related downstream messages are returned by the IConnectNotifyListener function described in the "Connection status and downstream message listeners" section of the Verify and connect a device topic.

Sample code:

// Subscribe to messages.
MqttSubscribeRequest subscribeRequest = new MqttSubscribeRequest();
// Replace subTopic with the topic to which you want to subscribe.
subscribeRequest.topic = subTopic;
subscribeRequest.isSubscribe = true;
subscribeRequest.qos = 0; // Valid values: 0 and 1.
LinkKit.getInstance().subscribe(subscribeRequest, new IConnectSubscribeListener() {
    @Override
    public void onSuccess() {
        // The subscription to the topic is successful.
    }
    @Override
    public void onFailure(AError aError) {
        // The subscription to the topic failed.
    }
});                      

If the device does not need to subscribe to the specified topic, you must unsubscribe from the topic. If you do not unsubscribe from the topic, the subscription persists and the device continues to receive messages from the subscribed topic. For more information about how to unsubscribe from a topic, see Unsubscribe from a topic.

Unsubscribe from a topic

// Unsubscribe from a topic.
MqttSubscribeRequest unsubRequest = new MqttSubscribeRequest();
// Replace unSubTopic with the topic from which you want to unsubscribe.
unsubRequest.topic = unSubTopic;
unsubRequest.isSubscribe = false;
LinkKit.getInstance().unsubscribe(unsubRequest, new IConnectUnscribeListener() {
    @Override
    public void onSuccess() {
        // The unsubscription from the topic is successful.
    }
    @Override
    public void onFailure(AError aError) {
        // The unsubscription from the topic failed.
    }
});