Android Stduio物联网实战(编写手机mqtt调试助手)

前提

导入mqtt的jar包并保证加入到dependencies中

1、布局文件activity_main.xml(只是几个EditText,button)



    
        
            
            
            
            
            
        

    

2、MainActicity.java(大部分注释都已表明)

package com.demo.mqtt_project;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
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 org.json.JSONException;
import org.json.JSONObject;

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

public class MainActivity extends AppCompatActivity {
    private String host,userName,mqtt_id,mqtt_sub_topic;
    private ScheduledExecutorService scheduler;
    private Button btn_test,btn_mqtt;
    public String Message_val;
    private MqttClient client;
    private MqttConnectOptions options;
    private Handler handler;
    private EditText user_mqtt,ip_mqtt,id_mqtt,topic_mqtt;
    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //这里是界面打开后 最先运行的地方
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // 对应界面UI
        id_mqtt = findViewById(R.id.id_mqtt);
        ip_mqtt = findViewById(R.id.ip_mqtt);
        user_mqtt = findViewById(R.id.user_mqtt);
        btn_mqtt =findViewById(R.id.btn_mqtt);
        topic_mqtt=findViewById(R.id.topic_mqtt);
        btn_test=findViewById(R.id.btn_1);
        btn_mqtt.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                host=ip_mqtt.getText().toString();
                mqtt_id=id_mqtt.getText().toString();
                userName=user_mqtt.getText().toString();
                mqtt_sub_topic=topic_mqtt.getText().toString();
        Mqtt_init();
        startReconnect();
        handler = new Handler() {
            @SuppressLint("SetTextI18n")
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==1){
                Message_val = (String) msg.obj;
                if (Message_val.contains(mqtt_sub_topic))
                {
                    String  msg_json = Message_val.substring(Message_val.indexOf("---")+3);
                    Toast.makeText(MainActivity.this, msg_json, Toast.LENGTH_SHORT).show();
                }
            } else if (msg.what == 2) {
                System.out.println("连接成功");
                Toast.makeText(MainActivity.this, "MQTT连接成功", Toast.LENGTH_SHORT).show();
                try {
                    client.subscribe(mqtt_sub_topic, 1);//订阅主题“PUE_Topic”
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (msg.what == 3) {
                Toast.makeText(MainActivity.this, "连接失败,系统正在重连", Toast.LENGTH_SHORT).show();
                System.out.println("连接失败,系统正在重连");
            }
            }
        };
            }
        });
        btn_test.setOnClickListener(new View.OnClickListener(){
            public void onClick(View s){
                publishmessageplus("ENTOPIC","hello world");
            }
            });
    }

    private void Mqtt_init()
    {
        try {
            //host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
            client = new MqttClient(host, mqtt_id,
                    new MemoryPersistence());
            //MQTT的连接设置
            options = new MqttConnectOptions();
            //设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
            options.setCleanSession(false);
            //设置连接的用户名
            options.setUserName(userName);
            // 设置超时时间 单位为秒
            options.setConnectionTimeout(10);
            // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
            options.setKeepAliveInterval(20);
            //设置回调
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    //连接丢失后,一般在这里面进行重连
                    System.out.println("connectionLost----------");
                    //startReconnect();
                }
                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    //publish后会执行到这里
                    System.out.println("deliveryComplete---------"
                            + token.isComplete());
                }
                @Override
                public void messageArrived(String topicName, MqttMessage message)
                        throws Exception {
                    //subscribe后得到的消息会执行到这里面
                    System.out.println("messageArrived----------");
                    Message msg = new Message();
                    //封装message包
                    msg.what = 1;   //收到消息标志位
                    msg.obj = topicName + "---" + message.toString();
                    //发送messge到handler
                    handler.sendMessage(msg);    // hander 回传
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    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 = 2;
                        // 没有用到obj字段
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 3;
                    // 没有用到obj字段
                    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);
    }
    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();
        }
    }

}

3、实现效果

可以像MQTTX电脑客户端一样连接你任何想要的mqtt服务器并监听所订阅主题的消息,并添加了测试按钮,向指定主题发送“hello world”消息

4、近两天需完善的

(1)、ui好看一点,如增加启动页面,连接页面在一个单独的activity中

(2)、可以订阅多主题

(3)、自由向主题发送消息等

你可能感兴趣的:(Android学习笔记,android,物联网,java)