mqtt客户端

1、首先在

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'

2、清单文件



    
    
    
    
    
    
        
            
                

                
            
        
        
        
        
    

3、配置类

package com.example.myapplication;

public class Api {
    public static String host="tcp://192.168.9.1:1883";
    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="2848444745_ESP";//发布主题
}

4、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;

/*
APP远程控制硬件点灯
发布消息给硬件订阅的TOPIC
开灯:{"set_led":1}
关灯:{"set_led":0}

硬件上报温度给手机APP
发布消息给APP订阅的TOPIC
{"temperature":23}


 */

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) {
//                System.out.println("hello");
//
//                Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();
//            }
//        });
//        image_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());
                        //MQTT 收到消息回传   UTF8Buffer msg=new UTF8Buffer(object.toString());
                        //处理message 传过来的 obj字段(里面包了数据)

//
                        //  String T_val = msg.obj.toString().substring(msg.obj.toString().indexOf("temperature\":") + 13, msg.obj.toString().indexOf("}"));
//                        String text_val = "温度:" + T_val;
//                        //在主进程 handler 里面更新UI  既保证了稳定性  又不影响网络传输
//                        text_test.setText(text_val);
//                        //Toast.makeText(MainActivity.this,T_val ,Toast.LENGTH_SHORT).show();

                        break;
                    case 30:  //连接失败
                        Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                        Log.d("网关连接状态", "连接失败");
                        break;
                    case 31:   //连接成功
                        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 = topicName + "---" + 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 = 31;
                        // 没有用到obj字段
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 30;
                    // 没有用到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);
    }


    /***
     * 发布消息加
     * @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,android,studio)