Android集成MQTT做一个客户端

前言

基于IDEA做的Android的MQTT客户端开发,没有运用太多android技术,纯净版开发。不做过多MQTT的介绍及服务器的配置,直接上代码。


编译开始

第一步,新建一个Android项目,在项目目录下的build.gradle中的repositories添加如下设置:

maven {
            url "https://repo.eclipse.org/content/repositories/paho-releases/"
        }

第二步,app目录下的build.gradle中的dependencies引入:

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

第三步,在AndroidManifest中的manifest标签加入如下权限:

    
        
	
	
	
	
	

第四步,代码实现:

为了方便测试我写了一个MQTT实体类,目的是默认填写一个服务器连接。需要写新的连接可以new对象。

/**
 * MQTT连接实体类,作用是自定义配置连接信息,以实现默认链接
 * 构造方法一定要写!
 * @author Peng
 */
public class MQTTentity {
    //以下请自己配置否则MainActivity中button_login的监听会有问题
    private String host;
    private String userName;
    private String passWord;
    private String timeTopic;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getTimeTopic() {
        return timeTopic;
    }

    public void setTimeTopic(String timeTopic) {
        this.timeTopic = timeTopic;
    }

    public MQTTentity() {
    }

    public MQTTentity(String host, String userName, String passWord, String timeTopic) {
        this.host = host;
        this.userName = userName;
        this.passWord = passWord;
        this.timeTopic = timeTopic;
    }

    @Override
    public String toString() {
        return "MQTTentity{" +
                "host='" + host + '\'' +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", timeTopic='" + timeTopic + '\'' +
                '}';
    }
}

然后在MainActivity中先声明MQTT实体和相关配置类:

    //我们自己新建的MQTT实体类
    private MQTTentity mqttentity;
    private MqttClient client;
    private MqttConnectOptions options;
    //以下两个声明目的是为了实现MQTT消息在testview刷新
    private ScheduledExecutorService scheduler;
    private Handler handler;

同时别忘了声明自己定义的button、textview、edittext等控件。

为了保证每个客户端在安装的时候不会冲突,在获取MQTT连接的时候不会出错,需要先写一个获取IMEI的方法,保证各个client唯一。

    /**
     * 获取手机imei
     * @param context
     * @param slotId
     * @return
     */
    public static String getIMEI(Context context, int slotId) {
        try {
            TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method method = manager.getClass().getMethod("getImei", int.class);
            String imei = (String) method.invoke(manager, slotId);
            return imei;
        } catch (Exception e) {
            return "";
        }
    }

接下来对MQTT进行初始化,我编了一个init()方法:

    /**
     * MQTT初始化连接
     */
    private void init(MQTTentity mqttentity) {
        try {
            String imei = getIMEI(MainActivity.this, 0);
            //host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
            client = new MqttClient(mqttentity.getHost(), imei, new MemoryPersistence());
            //MQTT的连接设置
            options = new MqttConnectOptions();
            //设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
            options.setCleanSession(true);
            //设置连接的用户名
            options.setUserName(mqttentity.getUserName());
            //设置连接的密码
            options.setPassword(mqttentity.getPassWord().toCharArray());
            // 设置超时时间 单位为秒
            options.setConnectionTimeout(10);
            // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
            options.setKeepAliveInterval(20);
            //设置回调
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    //连接丢失后,一般在这里面进行重连
                    System.out.println("connectionLost----------");
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    //publish后会执行到这里
                    System.out.println("deliveryComplete---------"
                            + token.isComplete());
                }

                @Override
                public void messageArrived(String topicName, MqttMessage message) {
                    //subscribe后得到的消息会执行到这里面
                    System.out.println("messageArrived----------");
                    Message msg = new Message();
                    msg.what = 1;   //收到消息标志位
                    msg.obj = topicName + "_" + message.toString();
                    handler.sendMessage(msg);    // hander 回传
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

初始化完成后就是建立连接并设置重连,以确保连接断开后自动重连:

    /**
     * MQTT建立连接及重连
     */
    private void startReconnect() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (!client.isConnected()) {
                    connect();
                }
            }
        }, 0, 10 * 1000, TimeUnit.MILLISECONDS);
    }

以上基本完成所有工作,先看下效果图:

Android集成MQTT做一个客户端_第1张图片Android集成MQTT做一个客户端_第2张图片Android集成MQTT做一个客户端_第3张图片

下面贴上MainActivity,XML布局就不贴了,自己设置布局并给每个控件添加ID就好:

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.lang.reflect.Method;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    //服务器参数配置
    private MQTTentity mqttentity;
    private MqttClient client;
    private MqttConnectOptions options;
    private ScheduledExecutorService scheduler;
    private Handler handler;

    private TextView textView;
    private EditText textView_intip;
    private EditText textView_intid;
    private EditText textView_intpwd;
    private EditText textView_inttopic;
    private EditText textView_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text_show);
        textView_intip = findViewById(R.id.textView_intip);
        textView_intid = findViewById(R.id.textView_intid);
        textView_intpwd = findViewById(R.id.textView_intpwd);
        textView_inttopic = findViewById(R.id.textView_inttopic);
        textView_send = findViewById(R.id.textView_send);

        Button button_defaultlogin = findViewById(R.id.button_defaultlogin);
        Button button_login = findViewById(R.id.button_login);
        Button button_publish = findViewById(R.id.button_publish);
        button_defaultlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mqttentity = new MQTTentity();
                init(mqttentity);
                startReconnect();
            }
        });
        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String host = textView_intip.getText().toString();
                String id = textView_intid.getText().toString();
                String pwd = textView_intpwd.getText().toString();
                String topic = textView_inttopic.getText().toString();
                if (host.length() != 0 && id.length() != 0 && pwd .length() != 0 && topic .length() != 0){
                    mqttentity = new MQTTentity(host,id,pwd,topic);
                    init(mqttentity);
                    startReconnect();
                }else {
                    Toast.makeText(MainActivity.this, "请输入完整连接信息!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        button_publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String send = textView_send.getText().toString();
                if (mqttentity != null && send.length() != 0){
                    publish(mqttentity.getTimeTopic(),send);
                }else {
                    Toast.makeText(MainActivity.this, "请检查连接或信息!", Toast.LENGTH_SHORT).show();
                }
            }
        });

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 1){
                    Toast.makeText(MainActivity.this, (String) msg.obj, Toast.LENGTH_SHORT).show();
                    textView.setText((String) msg.obj);
                }else if (msg.what == 2){
                    try {
                        client.subscribe(mqttentity.getTimeTopic(), 1);//订阅主题“timeTopic”
                        Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else if (msg.what == 3){
                    Toast.makeText(MainActivity.this, "连接失败,系统正在重连", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
    }

    /**
     * MQTT初始化连接
     */
    private void init(MQTTentity mqttentity) {
        try {
            String imei = getIMEI(MainActivity.this, 0);
            //host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
            client = new MqttClient(mqttentity.getHost(), imei, new MemoryPersistence());
            //MQTT的连接设置
            options = new MqttConnectOptions();
            //设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
            options.setCleanSession(true);
            //设置连接的用户名
            options.setUserName(mqttentity.getUserName());
            //设置连接的密码
            options.setPassword(mqttentity.getPassWord().toCharArray());
            // 设置超时时间 单位为秒
            options.setConnectionTimeout(10);
            // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
            options.setKeepAliveInterval(20);
            //设置回调
            client.setCallback(new MqttCallback() {
                @Override
                public void connectionLost(Throwable cause) {
                    //连接丢失后,一般在这里面进行重连
                    System.out.println("connectionLost----------");
                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken token) {
                    //publish后会执行到这里
                    System.out.println("deliveryComplete---------"
                            + token.isComplete());
                }

                @Override
                public void messageArrived(String topicName, MqttMessage message) {
                    //subscribe后得到的消息会执行到这里面
                    System.out.println("messageArrived----------");
                    Message msg = new Message();
                    msg.what = 1;   //收到消息标志位
                    msg.obj = topicName + "_" + message.toString();
                    handler.sendMessage(msg);    // hander 回传
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取手机imei
     * @param context
     * @param slotId
     * @return
     */
    public static String getIMEI(Context context, int slotId) {
        try {
            TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method method = manager.getClass().getMethod("getImei", int.class);
            String imei = (String) method.invoke(manager, slotId);
            return imei;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * MQTT建立连接及重连
     */
    private void startReconnect() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (!client.isConnected()) {
                    connect();
                }
            }
        }, 0, 10 * 1000, TimeUnit.MILLISECONDS);
    }

    /**
     * MQTT连接状态鉴别
     */
    private void connect() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    client.connect(options);
                    Message msg = new Message();
                    msg.what = 2;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 3;
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }

    /**
     * 向Topic发送消息
     * @param topic
     * @param sendMessage
     */
    public void publish(String topic,String sendMessage){
        Integer qos = 0;
        Boolean retained = false;
        try {
            if (client != null){
                client.publish(topic, sendMessage.getBytes(), qos.intValue(), retained.booleanValue());
            }
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            scheduler.shutdown();
            client.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}

以上参考于博主asjqkkkk的Android开发之MQTT的使用。他使用的是绑定服务的方式,我不太会,做了修改直接在MainActivity中做的部署。

你可能感兴趣的:(JAVA,Android)