MQTT_yw_Aidl使用教程

目录

项目地址:https://gitee.com/fish_five/mqtt_yw_aidl

1. 配置项目

1.1放置aidl文件

1.2根据gradle文件配置项目

1.3 同步和编译项目

2. 使用aidl接口

2.1 MQTT_yw_Aidl的三种场景

1. Imqttbgset模式

2. Imqttbgset2模式

3. Imqttbgset3模式

2.2 基本配置,请直接复制粘贴

1.mqttbgset模式

2.mqttbgset2模式

3.mqttbgset3模式

2.3 构建接收回调

2.4 开始连接

1.mqttbgset模式

2.mqttbgset2模式

3.mqttbgset3模式

2.5 订阅消息

1.mqttbgsetmqttbgset2模式

2.mqttbgset3模式

2.6 推送消息

1.mqttbgsetmqttbgset2模式

2.mqttbgset3模式

2.7断开连接

3. 运行配置

3.1下载并安装MQTT_yw

Gitee下载:

官网下载

3.2 项目运行要求

 


项目地址:https://gitee.com/fish_five/mqtt_yw_aidl

1. 配置项目

1.1放置aidl文件

将文件解压至你Android项目文件夹app\src\main的目录下

1.2根据gradle文件配置项目

高版本gradle在清单文件(AndroidManifest.xml)中添加:

 <queries>

      <package android:name="bhygzs.cn.mqtt_client" />

 </queries>

 

低版本gradlebuild.gradle(Module)文件中的targetSdk版本修改为29

 

如何判断gradle为高版本还是低版本?很简单,先按高版本配置,然后编译项目出错,就去用低版本方法。

1.3 同步和编译项目

Sync+Make+Rebuild

 

2. 使用aidl接口

2.1 MQTT_yw_Aidl的三种场景

1. Imqttbgset模式

适用无需用户名密码认证或者采用clientid认证的MQTT通信连接。

2. Imqttbgset2模式

适用采用用户名/密码认证的MQTT通信连接。

3. Imqttbgset3模式

适用用户名密码认证且对消息等级有要求以及经常断线重连的MQTT通信连接。

 

2.2 基本配置,请直接复制粘贴

1.mqttbgset模式

1. private Imqttbgset imqttbgset;  

2.   

3. private void onBind() {  

4.     Intent intent = new Intent();  

5.     intent.setAction("bhygzs.cn.mqtt_client.AIDL");  

6.     intent.addCategory(Intent.CATEGORY_DEFAULT);  

7.     intent.setPackage("bhygzs.cn.mqtt_client");  

8.     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

9.     bindService(intent, new AidlConnection(), BIND_AUTO_CREATE);  

10. }  

11.   

12. private class AidlConnection implements ServiceConnection {  

13.     @Override  

14.         public void onServiceConnected(ComponentName name, IBinder service) {  

15.         imqttbgset = Imqttbgset.Stub.asInterface(service);  

16.     }  

17.     @Override  

18.         public void onServiceDisconnected(ComponentName name) {  

19.     }  

20. }  

2.mqttbgset2模式

1. private Imqttbgset2 imqttbgset;  

2.   

3. private void onBind() {  

4.     Intent intent = new Intent();  

5.     intent.setAction("bhygzs.cn.mqtt_client.AIDL2");  

6.     intent.addCategory(Intent.CATEGORY_DEFAULT);  

7.     intent.setPackage("bhygzs.cn.mqtt_client");  

8.     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

9.     bindService(intent, new MQTT2Activity.AidlConnection(), BIND_AUTO_CREATE);  

10. }  

11.   

12. private class AidlConnection implements ServiceConnection {  

13.   

14.     @Override  

15.     public void onServiceConnected(ComponentName name, IBinder service) {  

16.         imqttbgset = Imqttbgset2.Stub.asInterface(service);  

17.     }  

18.   

19.     @Override  

20.     public void onServiceDisconnected(ComponentName name) {  

21.   

22.     }  

23. }  

3.mqttbgset3模式

1. private Imqttbgset3 imqttbgset;  

2.   

3. private void onBind() {  

4.       Intent intent = new Intent();  

5.       intent.setAction("bhygzs.cn.mqtt_client.AIDL3");  

6.       intent.addCategory(Intent.CATEGORY_DEFAULT);  

7.       intent.setPackage("bhygzs.cn.mqtt_client");  

8.       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

9.       bindService(intent, new MQTT3Activity.AidlConnection(), BIND_AUTO_CREATE);  

10.   }  

11.   

12. private class AidlConnection implements ServiceConnection {  

13.   

14.       @Override  

15.       public void onServiceConnected(ComponentName name, IBinder service) {  

16.           imqttbgset = Imqttbgset3.Stub.asInterface(service);  

17.       }  

18.   

19.       @Override  

20.       public void onServiceDisconnected(ComponentName name) {  

21.   

22.       }  

23.   }  

 

 

注意:onBind函数记得在onCreate调用,imqttbgset 记得声明为全局变量。

 

2.3 构建接收回调

1. private class callback extends Imqttbgret.Stub {  

2.   

3.      @Override  

4.      public void retServermsg(String ss) throws RemoteException {  

5.          tv1.setText(String.format("【服务器消息:{%s}】", ss));  

6.      }  

7.   

8.      @Override  

9.      public void retTopicmsg(String ss) throws RemoteException {  

10.          tv1.setText(String.format("【订阅消息:{%s}】", ss));  

11.      }  

12.  }  

 

 

2.4 开始连接

1.mqttbgset模式

1. imqttbgset.connect("tcp://域名或ip:1883""你的clientID"new callback());  

2.mqttbgset2模式

1. imqttbgset.connect("tcp://域名或ip:1883""你的clientID","用户名","密码",new callback());  

3.mqttbgset3模式

1. imqttbgset.connect("tcp://域名或ip:1883""你的clientID",强制在线多少小时,"用户名","密码",new callback());    

2.5 订阅消息

1.mqttbgsetmqttbgset2模式

1. imqttbgset.set_retopic("主题");  

2.mqttbgset3模式

1. imqttbgset.set_retopic("主题"qos等级);  

2. //qos有效值:1,2,3  

2.6 推送消息

1.mqttbgsetmqttbgset2模式

1. imqttbgset.fs_toserver("主题"“主题内容”);  

2.mqttbgset3模式

1. imqttbgset.fs_toserver("主题"qos等级,“主题内容”);  

2. //qos有效值:1,2,3  

2.7断开连接

1. imqttbgset.disconnect();  

 

3. 运行配置

3.1下载并安装MQTT_yw

Gitee下载:https://gitee.com/fish_five/mqtt_yw_aidl/releases/MQTT_yw

官网下载

https://www.cbug.top/article/13.html

3.2 项目运行要求

1.手机设置关联启动管理,为MQTT_yw App开启该权限。

2.如果你的手机没有上述该功能,请先打开MQTT_yw App,并让其保持后台运行。