安卓MQTT基本使用

一在 build.gradle 添加相应依赖

  implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

二、添加权限

    
    
    
    
    

三、配置订阅信息  (订阅信息更具你的实际情况进行修改)

package com.example.myapplication;

public class Api {
    public static String host="tcp://192.168.9.2:1885";
    public static String userName="admin";
    public static String passWord="password";
    public static String mqtt_id="mqtt_brokerr";
    public static String mqtt_sub_topic="{app}/set/request/database/model";订阅主题
    public static String mqtt_pub_topic="2548444745_esp";//发布主题
}

Mactivity 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;



public class MainActivity extends AppCompatActivity {
    private int led_flag = 1;
    private ScheduledExecutorService scheduler;
    private Button btn_1;
    private TextView text_test;


    private MqttClient client;
    private MqttConnectOptions options;

    private Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //这里是界面打开后 最先运行的地方
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1 = findViewById(R.id.btn_1);


       btn_1.setOnClickListener(new View.OnClickListener() {
         @Override
           public void onClick(View v) {
               if (led_flag == 0) {
                  publishmessageplus(mqtt_pub_topic, "{\"set_led\":1}");
                   led_flag = 1;
               } else {
                  publishmessageplus(mqtt_pub_topic, "{\"set_led\":0}");
                  led_flag = 0;
               }
           }
        });
        text_test = findViewById(R.id.text_test);
//**********************************************************//
        Mqtt_init();
        startReconnect();
        handler = new Handler() {
            @SuppressLint("SetTextI18n")
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        break;
                    case 2:  

                        break;
                    case 3:
                        Log.d("收到消息回传==============: ", msg.toString());




                   //在主进程 handler 里面更新UI  既保证了稳定性  又不影响网络传输
                       text_test.setText(msg.toString());


                        break;
                    case 30:  //连接失败
                        Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                        Log.d("连接状态", "连接失败");
                        break;
                    case 35:   //连接成功
                        Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                        Log.d("连接状态", "连接成功");
                        //订阅主题
                        try {
                            client.subscribe(Api.mqtt_sub_topic, 1);
                        } catch (MqttException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        break;
                }
            }
        };
    }


    /***
     * MQTT 初始化
     */
    private void Mqtt_init() {
        try {

            //host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
            client = new MqttClient(Api.host, Api.mqtt_id,
                    new MemoryPersistence());
            //MQTT的连接设置
            options = new MqttConnectOptions();
            //设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
            options.setCleanSession(false);
            options.setUserName(Api.userName);
            options.setPassword(Api.passWord.toCharArray());
            // 设置超时时间 单位为秒
            options.setConnectionTimeout(10);
            // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
            options.setKeepAliveInterval(20);
            //设置回调
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    //连接丢失后,一般在这里面进行重连
                    Log.d("连接状态", "连接丢失");
                    startReconnect();
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    //publish后会执行到这里
                    Log.d("连接状态", "交付完成:" + token.isComplete());
                }

                @Override
                public void messageArrived(String topicName, MqttMessage message)
                        throws Exception {
                    //subscribe后得到的消息会执行到这里面
                    Log.d("连接状态", "消息到达");
                    Message msg = new Message();
                    //封装message包
                    msg.what = 3;   //收到消息标志位
                    msg.obj =  message.toString();
                    //发送messge到handler
                    handler.sendMessage(msg);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * MQTT 连接
     */
    private void Mqtt_connect() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (!(client.isConnected()))  //如果还未连接
                    {
                        client.connect(options);
                        Message msg = new Message();
                        msg.what = 35;
                  
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 30;
                 
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }


    /***
     * 开始重新连接
     */
    private void startReconnect() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (!client.isConnected()) {
                    Mqtt_connect();
                }
            }
        }, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS);
    }


    /***
     * 发布消息加
     * @param topic
     * @param message2
     */
    private void publishmessageplus(String topic, String message2) {
       if (client == null || !client.isConnected()) {
          return;
       }
       MqttMessage message = new MqttMessage();
        message.setPayload(message2.getBytes());
        try {
          client.publish(topic, message);
      } catch (MqttException e) {

          e.printStackTrace();
        }
    }


}

布局代码



    

            
            
                
                
                
                

            

        

    






你可能感兴趣的:(安卓静默安装,android,java,开发语言)