项目地址:https://gitee.com/fish_five/mqtt_yw_aidl
将文件解压至你Android项目文件夹app\src\main的目录下。
高版本gradle在清单文件(AndroidManifest.xml)中添加:
<queries>
<package android:name="bhygzs.cn.mqtt_client" />
</queries>
低版本gradle将build.gradle(Module)文件中的targetSdk版本修改为29。
如何判断gradle为高版本还是低版本?很简单,先按高版本配置,然后编译项目出错,就去用低版本方法。
Sync+Make+Rebuild
适用无需用户名密码认证或者采用clientid认证的MQTT通信连接。
适用采用用户名/密码认证的MQTT通信连接。
适用用户名密码认证且对消息等级有要求以及经常断线重连的MQTT通信连接。
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. }
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. }
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 记得声明为全局变量。
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. }
1. imqttbgset.connect("tcp://域名或ip:1883", "你的clientID", new callback());
1. imqttbgset.connect("tcp://域名或ip:1883", "你的clientID","用户名","密码",new callback());
1. imqttbgset.connect("tcp://域名或ip:1883", "你的clientID",强制在线多少小时,"用户名","密码",new callback());
1. imqttbgset.set_retopic("主题");
1. imqttbgset.set_retopic("主题",qos等级);
2. //qos有效值:1,2,3
1. imqttbgset.fs_toserver("主题",“主题内容”);
1. imqttbgset.fs_toserver("主题",qos等级,“主题内容”);
2. //qos有效值:1,2,3
1. imqttbgset.disconnect();
Gitee下载:https://gitee.com/fish_five/mqtt_yw_aidl/releases/MQTT_yw
https://www.cbug.top/article/13.html
1.手机设置关联启动管理,为MQTT_yw App开启该权限。
2.如果你的手机没有上述该功能,请先打开MQTT_yw App,并让其保持后台运行。