ESA2GJK1DH1K Basics: Android connection MQTT simple Demo

 

 

 

Digression

   My dad asked me things trend is what !!!!!!

  :( My own feeling is heading in the direction of the server, "I, Robot" the film)

  And server equipment will interact in the future, that is no longer do local processing, all the data is processed in a huge server

  We now after various software on your phone will not be installed in the phone, all are on the server side

  Now the small micro-channel program, Ali applets, etc., it is in the applications into their server-side run.

  This way the test run two ways:

    1. server capacity

    2. Network data communication speed

  As long as these two areas are reached, it will make a lot of innovative new generation (like computers and mobile phones), the current will also encourage other industries to develop.

Foreword

  This section let everyone know how Android is connected MQTT

  Using the following package

  

  Official Download

  https://docs.emqx.io/sdk_tools?category=MQTT_Clients

  

 

New construction, import jar package

jar package copied to the app / libs directory of the new project

 

 

 

 

 

 

Software settings are loaded jar package

 

 

 

 

 

 

 

 

 

 

Connection MQTT

public class MainActivity extends AppCompatActivity {

    private static MqttClient mqttClient;
    private static MqttConnectOptions mqttConnectOptions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mqttConnectOptions  = new MqttConnectOptions();//MQTT的连接设置
        mqttConnectOptions.setCleanSession(true);//清除连接信息
        mqttConnectOptions.setUserName("yang");//设置连接的用户名
        mqttConnectOptions.setPassword("11223344".toCharArray());//设置连接的密码
        mqttConnectOptions.setConnectionTimeout(3);// 设置连接超时时间 单位为秒
        mqttConnectOptions.setKeepAliveInterval(60);//心跳包时间60S
        try { mqttClient = new MqttClient(
                    "tcp://47.92.31.46:1883",//连接的地址信息
                    System.currentTimeMillis()+"",//ClientID,使用当前时间戳
                    new MemoryPersistence());//
            mqttClient.setCallback(new MqttCallback() {//回调函数
                @Override//连接断开
                public void connectionLost(Throwable throwable) { }
                @Override//接收到消息
                public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { }
                @Override//没用过
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { }
            });
            new Thread(new Runnable() {//用任务
                @Override
                public void run() {
                    try {
                        mqttClient.connect(mqttConnectOptions);
                        if (mqttClient.isConnected()) {
                            Log.e("Mqtt Info", "ConnectSuccess");
                        }
                    }catch (Exception e){
                        Log.e("Mqtt Err", e.toString());
                    }
                }
            }).start();
        }
        catch (Exception e){
            Log.e("Mqtt Err", e.toString());
        }
    }
}

 

 

 

 

 

 

 

 

 

添加网络通信权限

<uses-permission android:name="android.permission.INTERNET" />

 

 

 

 

下载到手机测试

 

订阅主题,打印接收的消息

Log.e("MqttMsg:", "Topic:"+s+" "+"Msg:"+mqttMessage.toString());
mqttClient.subscribe("1111",0);//订阅1111,消息等级0
 

 

 

 

测试

 

 

 

发送消息

final MqttMessage msgMessage = new MqttMessage(mqttMessage.toString().getBytes());
new Thread(new Runnable() {//用任务
@Override
public void run() {
try {
mqttClient.publish("2222",msgMessage);//发布主题为2222的消息
}catch (Exception e){
Log.e("Mqtt Err", e.toString());
}
}
}).start();

 

 

 

 

 

 

 

 

测试

 

 

Guess you like

Origin www.cnblogs.com/yangfengwu/p/11839484.html