Android的MQTT客户端实现

最近做的传感器课程设计配上了MQTT传输协议,之前一直都是用的TCP,用多了就感觉挺麻烦哈哈,尝试一下物联网界人人称好的MQTT协议,具体怎么搭建MQTT服务器我之前的博文有写
Apache-artemis的MQTT服务器搭建、启动、使用
下面进入正题

添加依赖包

项目的build.gradle中添加

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

基于eclipse的一个MQTT快速开发包吧好像是
然后在app的build.gradle中的dependencies中添加

dependencies {
    
    
    compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
}

ps:安卓小白请自行学习安卓添加依赖包的方式,当然也有jar包的安装方式,我嫌麻烦就没用,嘻嘻

全部添加完了就Sync Now开始构建依赖,然后就自动帮你下载依赖包了,等两分钟后就可以看到成功啦
在这里插入图片描述

添加权限

权限直接复制粘贴吧,我个人比较喜欢直接粘贴权限进去,当然也有IDE添加方式,个人觉得不好用就罢了

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

把以上权限添加到AndroidManifest.xml不会添加的请自行回去好好学学基础,以上权限暂时还没发现6.0系统以上需要动态设置

开启服务

同样在AndroidManifest.xml中的application标签内注册服务

    <!-- Mqtt服务 -->
    <!--这里有个坑,这里需要注册两个服务,一个是MQTT包的服务,一个是自己那个MQTTservice的服务-->
    <service android:name=".MQTTService" />
    <service android:name="org.eclipse.paho.android.service.MqttService" />

到此,配置已经全部完成了,接下来是代码实现部分

代码实现

由于MQTT通信的特殊性,为了确保他可以及时接收到订阅频道的消息,那就肯定要基于Service去封装一个MQTT配置及Util类

MQTTService.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class MQTTService extends Service {
    
    

    public static final String TAG = MQTTService.class.getSimpleName();

    private static MqttAndroidClient client;
    private MqttConnectOptions conOpt;

    private String host = "tcp://192.168.0.11:61613";
    private String userName = "admin";
    private String passWord = "password";
    private static String myTopic = "ForTest";      //要订阅的主题
    private String clientId = "androidId";//客户端标识
    private IGetMessageCallBack IGetMessageCallBack;


    @Override
    public void onCreate() {
    
    
        super.onCreate();
        Log.e(getClass().getName(), "onCreate");
        init();
    }

    public static void publish(String msg){
    
    
        String topic = myTopic;
        Integer qos = 0;
        Boolean retained = false;
        try {
    
    
            if (client != null){
    
    
                client.publish(topic, msg.getBytes(), qos.intValue(), retained.booleanValue());
            }
        } catch (MqttException e) {
    
    
            e.printStackTrace();
        }
    }

    private void init() {
    
    
        // 服务器地址(协议+地址+端口号)
        String uri = host;
        client = new MqttAndroidClient(this, uri, clientId);
        // 设置MQTT监听并且接受消息
        client.setCallback(mqttCallback);

        conOpt = new MqttConnectOptions();
        // 清除缓存
        conOpt.setCleanSession(true);
        // 设置超时时间,单位:秒
        conOpt.setConnectionTimeout(10);
        // 心跳包发送间隔,单位:秒
        conOpt.setKeepAliveInterval(20);
        // 用户名
        conOpt.setUserName(userName);
        // 密码
        conOpt.setPassword(passWord.toCharArray());     //将字符串转换为字符串数组

        // last will message
        boolean doConnect = true;
        String message = "{\"terminal_uid\":\"" + clientId + "\"}";
        Log.e(getClass().getName(), "message是:" + message);
        String topic = myTopic;
        Integer qos = 0;
        Boolean retained = false;
        if ((!message.equals("")) || (!topic.equals(""))) {
    
    
            // 最后的遗嘱
            // MQTT本身就是为信号不稳定的网络设计的,所以难免一些客户端会无故的和Broker断开连接。
            //当客户端连接到Broker时,可以指定LWT,Broker会定期检测客户端是否有异常。
            //当客户端异常掉线时,Broker就往连接时指定的topic里推送当时指定的LWT消息。

            try {
    
    
                conOpt.setWill(topic, message.getBytes(), qos.intValue(), retained.booleanValue());
            } catch (Exception e) {
    
    
                Log.i(TAG, "Exception Occured", e);
                doConnect = false;
                iMqttActionListener.onFailure(null, e);
            }
        }

        if (doConnect) {
    
    
            doClientConnection();
        }

    }


    @Override
    public void onDestroy() {
    
    
        stopSelf();
        try {
    
    
            client.disconnect();
        } catch (MqttException e) {
    
    
            e.printStackTrace();
        }
        super.onDestroy();
    }

    /** 连接MQTT服务器 */
    private void doClientConnection() {
    
    
        if (!client.isConnected() && isConnectIsNormal()) {
    
    
            try {
    
    
                client.connect(conOpt, null, iMqttActionListener);
            } catch (MqttException e) {
    
    
                e.printStackTrace();
            }
        }

    }

    // MQTT是否连接成功
    private IMqttActionListener iMqttActionListener = new IMqttActionListener() {
    
    

        @Override
        public void onSuccess(IMqttToken arg0) {
    
    
            Log.i(TAG, "连接成功 ");
            try {
    
    
                // 订阅myTopic话题
                client.subscribe(myTopic,1);
            } catch (MqttException e) {
    
    
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(IMqttToken arg0, Throwable arg1) {
    
    
            arg1.printStackTrace();
            // 连接失败,重连
        }
    };

    // MQTT监听并且接受消息
    private MqttCallback mqttCallback = new MqttCallback() {
    
    

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
    
    

            String str1 = new String(message.getPayload());
            if (IGetMessageCallBack != null){
    
    
                IGetMessageCallBack.setMessage(str1);
            }
            String str2 = topic + ";qos:" + message.getQos() + ";retained:" + message.isRetained();
            Log.i(TAG, "messageArrived:" + str1);
            Log.i(TAG, str2);
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken arg0) {
    
    

        }

        @Override
        public void connectionLost(Throwable arg0) {
    
    
            // 失去连接,重连
        }
    };

    /** 判断网络是否连接 */
    private boolean isConnectIsNormal() {
    
    
        ConnectivityManager connectivityManager = (ConnectivityManager) this.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
    
    
            String name = info.getTypeName();
            Log.i(TAG, "MQTT当前网络名称:" + name);
            return true;
        } else {
    
    
            Log.i(TAG, "MQTT 没有可用网络");
            return false;
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
    
    
        Log.e(getClass().getName(), "onBind");
        return new CustomBinder();
    }

    public void setIGetMessageCallBack(IGetMessageCallBack IGetMessageCallBack){
    
    
        this.IGetMessageCallBack = IGetMessageCallBack;
    }

    public class CustomBinder extends Binder {
    
    
        public MQTTService getService(){
    
    
            return MQTTService.this;
        }
    }

    public  void toCreateNotification(String message){
    
    
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(this,MQTTService.class), PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);//3、创建一个通知,属性太多,使用构造器模式

        Notification notification = builder
                .setTicker("test_title")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("")
                .setContentText(message)
                .setContentInfo("")
                .setContentIntent(pendingIntent)//点击后才触发的意图,“挂起的”意图
                .setAutoCancel(true)        //设置点击之后notification消失
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        startForeground(0, notification);
        notificationManager.notify(0, notification);

    }
}

到这里呢,复制党们就要喷了,你这不对啊,还差一个接口标红啊,【手动白眼】创建一个呗,来吧复制党

public interface IGetMessageCallBack {
    
    
    public void setMessage(String message);
}

要是不会创建接口呢,就别复制了吧,好好学学基础
下面,为了方便Service与Acitivity之间的通信,创建一个工具类作为桥梁
MyServiceConnection.java

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;

public class MyServiceConnection implements ServiceConnection {
    
    

    private MQTTService mqttService;
    private IGetMessageCallBack IGetMessageCallBack;

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    
    
        mqttService = ((MQTTService.CustomBinder)iBinder).getService();
        mqttService.setIGetMessageCallBack(IGetMessageCallBack);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
    
    

    }

    public MQTTService getMqttService(){
    
    
        return mqttService;
    }

    public void setIGetMessageCallBack(IGetMessageCallBack IGetMessageCallBack){
    
    
        this.IGetMessageCallBack = IGetMessageCallBack;
    }
}

好了,最后测试一下看看吧

public class MainActivity extends AppCompatActivity implements IGetMessageCallBack {
    
    

    private TextView textView;
    private Button button;
    private MyServiceConnection serviceConnection;
    private MQTTService mqttService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.text);
        button = (Button) findViewById(R.id.button);

        serviceConnection = new MyServiceConnection();
        serviceConnection.setIGetMessageCallBack(MainActivity.this);
		//用Intent方式创建并启用Service
        Intent intent = new Intent(this, MQTTService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

		//按下这个按钮就发布一条消息
        button.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                MQTTService.publish("Test");
            }
        });
    }
	
	//继承了这个接口就直接用这个借口的方法就好啦
    @Override
    public void setMessage(String message) {
    
    
        textView.setText(message);
        mqttService = serviceConnection.getMqttService();
        mqttService.toCreateNotification(message);
    }

    
    //这里最主要还是销毁掉服务,因为Activity销毁掉以后
    //Service并不会自动回收,而是会转入后台运行,这样会
    //影响到下一次的运行,所以这里必须做销毁处理
    @Override
    protected void onDestroy() {
    
    
        unbindService(serviceConnection);
        super.onDestroy();
    }
}

测试效果就不放了,主要自己太懒。
不要问我要代码哈,上面已经很详细了,懂得自然懂
不懂的,不会的,学好基础,学好基础。
以上内容仅做个人学习记录,不做学术性研究

补充

在实践过程中有可能会报错
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/localbroadcastmanager/content/LocalBroadcastManager;
在这里插入图片描述
在app的build中新增下面那个支持依赖包就好

 compile 'com.android.support:support-v4:28.0.0'

猜你喜欢

转载自blog.csdn.net/AcStudio/article/details/106323735