<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/ZLBC26AA/" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
新建工程
安装MQTT的Jar包
1.第一种方式
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
2.第二种,我准备好了jar包,可以直接导入 jar包
拷贝到自己工程的libs里面
把MyMqttClient.java 文件放到自己的工程
1.修改MQTT信息
2,增加网络权限
<uses-permission android:name="android.permission.INTERNET" />
连接MQTT
该代码写一次即可,后期断线自动重连
MyMqttClient.sharedCenter().setConnect();
订阅主题
MyMqttClient.sharedCenter().setSubscribe("1111",0);//订阅主题1111,消息等级0
建议使用方式
代码语言:javascript复制public class MainActivity extends AppCompatActivity {
//定时器用于轮训订阅主题
private Timer timerSubscribeTopic = null;
private TimerTask TimerTaskSubscribeTopic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyMqttClient.sharedCenter().setConnect();
/**
* 订阅主题成功回调
*/
MyMqttClient.sharedCenter().setOnServerSubscribeCallback(new MyMqttClient.OnServerSubscribeSuccessCallback() {
@Override
public void callback(String Topic, int qos) {
if (Topic.equals("1111")){//订阅1111成功
stopTimerSubscribeTopic();//订阅到主题,停止订阅
}
}
});
startTimerSubscribeTopic();//定时订阅主题
}
/**
* 定时器每隔1S尝试订阅主题
*/
private void startTimerSubscribeTopic(){
if (timerSubscribeTopic == null) {
timerSubscribeTopic = new Timer();
}
if (TimerTaskSubscribeTopic == null) {
TimerTaskSubscribeTopic = new TimerTask() {
@Override
public void run() {
MyMqttClient.sharedCenter().setSubscribe("1111",0);//订阅主题1111,消息等级0
}
};
}
if(timerSubscribeTopic != null && TimerTaskSubscribeTopic != null )
timerSubscribeTopic.schedule(TimerTaskSubscribeTopic, 0, 1000);
}
private void stopTimerSubscribeTopic(){
if (timerSubscribeTopic != null) {
timerSubscribeTopic.cancel();
timerSubscribeTopic = null;
}
if (TimerTaskSubscribeTopic != null) {
TimerTaskSubscribeTopic.cancel();
TimerTaskSubscribeTopic = null;
}
}
//当活动不再可见时调用
@Override
protected void onStop()
{
super.onStop();
stopTimerSubscribeTopic();//停止定时器订阅
}
/**
* 当处于停止状态的活动需要再次展现给用户的时候,触发该方法
*/
@Override
protected void onRestart() {
super.onRestart();
startTimerSubscribeTopic();//定时订阅主题
}
@Override
protected void onPause() {
super.onPause();
stopTimerSubscribeTopic();
}
}
取消订阅
MyMqttClient.sharedCenter().setUnSubscribe("1111");//取消订阅主题1111
接收消息
哪个文件需要获取MQTT数据,只需要按照下面的方式写上即可!
代码语言:javascript复制MyMqttClient.sharedCenter().setOnServerReadStringCallback(new MyMqttClient.OnServerReadStringCallback() {
@Override//Topic:主题 Msg.toString():接收的消息 MsgByte:16进制消息
public void callback(String Topic, MqttMessage Msg, byte[] MsgByte) {
Log.e("MqttMsg", "Topic: " Topic " Msg" Msg.toString() );
}
});
发布消息
1.发布字符串消息
MyMqttClient.sharedCenter().setSendData("2222","msg",0,false);//发送字符串消息
2222:发布的主题 msg:发布的消息 0:消息等级 false:不需要服务器记录
2.发布16进制消息
byte[] bytes= new byte[2];
bytes[0] = 0x03;
bytes[1] = 0x55;
MyMqttClient.sharedCenter().setSendData("3333",bytes,0,false);
监听MQTT连接状态
代码语言:javascript复制/*连接上MQTT*/
MyMqttClient.sharedCenter().setOnServerConnectedCallback(new MyMqttClient.OnServerConnectedCallback() {
@Override
public void callback() {
}
});
/*和MQTT服务器断开连接*/
MyMqttClient.sharedCenter().setOnServerDisConnectedCallback(new MyMqttClient.OnServerDisConnectedCallback() {
@Override
public void callback(Throwable e) {
}
});