android mqtt 客户端收发消息

参考网页:
https://blog.csdn.net/asjqkkkk/article/details/80714234   
https://blog.csdn.net/hbw020/article/details/79865397

(1)下载
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.android.service/1.1.1/org.eclipse.paho.android.service-1.1.1.jar
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.client.mqttv3/1.2.0/org.eclipse.paho.client.mqttv3-1.2.0.jar

(2)在文件管理器里面对着org.eclipse.paho.android.service-1.1.1.jar和org.eclipse.paho.client.mqttv3-1.2.0.jar点击右键,选择“复制(C)”
点击Project窗口的下拉箭头,选择Project
对着Project-->app-->libs点击右键,选择Copy,记得一个一个拷贝,不能两个一起拷贝!!

(3)20240123:新版好像改了,改成这样:
上面拷贝完后-->对着Project-->app-->libs-->org.eclipse.paho.android.service-1.1.1.jar点击右键-->Add As Libray,出现新的对话框-->*:app不用变,*代表你自己的名字-->点击OK
用同样的方法,添加org.eclipse.paho.client.mqttv3-1.2.0.jar

自动在android studio-->Gradle Scripts-->build.gradle(Module:app)-->添加了内容
org.eclipse.paho.android.service-1.1.1.jar的前面多了‘>’,可以点开,表示成功了。
很多时候会失败,删除build.gradle(Module:app)里面的对应的内容,然后重新来。多试几次就会成功。

********这步不用做了
点击File-->Project structure,打开一个窗口
在窗口里点击Dependencies-->Modules app-->All Dependencies 的加号-->JAR/AAR Dependency-->输入:libs\org.eclipse.paho.android.service-1.1.1.jar-->OK
同样的方式添加org.eclipse.paho.client.mqttv3-1.2.0.jar

********这步不用做了,作用应该是自动下载jar,但是现在已经手动下载了。
添加依赖
点击android studio-->Gradle Scripts-->build.gradle(Project:自己的项目名称)-->添加:
buildscript {
    repositories {
        google()
        mavenCentral()

        //这个也变了,20240123,
        //maven {
        //    url "https://repo.eclipse.org/content/repositories/paho-releases/"
        //}
        //变成这样:
        maven { url = uri("https://repo.eclipse.org/content/repositories/paho-releases/") }
    }
    //...
}

(4)声明权限
点击android studio-->app-->manifests-->AndroidManifest.xml-->添加:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myandroidmqtt">  <!--  这个也变了,反正下面的内容放在最前面就对了 !-->

    <!-- 注意不要放错位置! -->
    <!-- 添加下面6行 -->
    <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" />

    <application
        ... ...
    </application>
</manifest>

********这步不用做了,直接做(5),然后手动添加CMqttService.cpp
(5)开启服务:
对着android studio-->Android-->app-->manifests-->AndroidManifest.xml点击右键,
选择:New-->Service-->Service-->Class Name输入:CMqttService-->点击OK。
1.自动在AndroidManifest.xml里面添加<service ... MQTTServer ... />,内容如下:
        <service
            android:name=".CMqttService"
            android:enabled="true"
            android:exported="true"></service>
2.同时也会自动添加文件:CMqttService.cpp

(6)1.移动CMqttService.cpp的位置,然后添加代码
2.修改上面的内容,如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myandroidmqtt">

    <application
        ... ...
        <!-- ...  ... -->
        <!-- 注意不要放错位置! 自动添加的时候,没有name后面的东西。-->
        <!-- 如果不加com.example.stockmqtt.MQTTService会没有反应。-->
        <!-- 如果不加org.eclipse.paho.android.service.MqttService,会崩溃。-->
        <!-- 记得修改:com.example.stockmqtt.MQTTService。-->
        <service
            android:name="com.example.stockmqtt.MQTTService"
            android:enabled="true"
            android:exported="true"></service>
        <service android:name="org.eclipse.paho.android.service.MqttService" />
        <activity
             ... ...
        </activity>
    </application>

    <!-- ...  ... -->

</manifest>

(7)错误:
java.lang.RuntimeException: Unable to bind to service com.example.myandroidmqtt.MQTTService@5d91622 with Intent { cmp=com.example.myandroidmqtt/.MQTTService }: java.lang.UnsupportedOperationException: Not yet implemented
解决:
自动生成的代码如下:
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
MainActivity.java代码里面是下面这样的。这样就不会有问题:
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(getClass().getName(), "onBind");
        return new CustomBinder();
    }

(8)MainActivity.java代码

package com.example.myandroidmqtt;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myandroidmqtt.MyServiceConnection.IGetMessageCallBack;

public class MainActivity extends AppCompatActivity implements IGetMessageCallBack {


    private TextView textView;
    private Button button;

    //mqtt_service
    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);

        //mqtt_service
        serviceConnection = new MyServiceConnection();
        serviceConnection.setIGetMessageCallBack(MainActivity.this);
        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) {
                //mqtt_service
                MQTTService.publish("测试一下子");
            }
        });
    }

    //mqtt_service
    @Override
    public void setMessage(String message) {
        textView.setText(message);
        mqttService = serviceConnection.getMqttService();
        mqttService.toCreateNotification(message);
    }
    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        super.onDestroy();
    }
}

(9)MQTTService.java代码

package com.example.myandroidmqtt;

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.support.v4.app.NotificationCompat;
import android.util.Log;

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;

import com.example.myandroidmqtt.MyServiceConnection.IGetMessageCallBack;

public class MQTTService extends Service {
//  public MQTTService() {
//  }

//  @Override
//  public IBinder onBind(Intent intent) {
//      // TODO: Return the communication channel to the service.
//      throw new UnsupportedOperationException("Not yet implemented");
//  }


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


    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;
        }
    }

    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("测试标题")
                .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);

    }
}

(10)MyServiceConnection .java代码

package com.example.myandroidmqtt;

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 interface IGetMessageCallBack {
        public void setMessage(String message);
    }

}

(11)active_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="80dip"
        android:layout_height="50dip"
        android:layout_marginLeft="165dip"
        android:text="测试按钮"
        android:gravity="center"/>

</android.support.constraint.ConstraintLayout>

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值