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

前提

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

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="251dp"
            android:orientation="vertical">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:hint="请输入ip地址"
                android:id="@+id/ip_mqtt"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:hint="请输入Client_id"
                android:id="@+id/id_mqtt"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:hint="请输入User_name"
                android:id="@+id/user_mqtt"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:hint="请输入订阅主题"
                android:id="@+id/topic_mqtt"/>
            <Button
                android:layout_width="150dp"
                android:layout_height="50dp"
                android:text="点我连接"
                android:id="@+id/btn_mqtt"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_marginTop="5dp"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:text="测试按钮"
                android:id="@+id/btn_1"
                android:layout_height="wrap_content">
            </Button>
        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

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)、自由向主题发送消息等

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值