package com.lotaai.canguiayw.mqtt;
|
|
import android.app.Service;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.os.IBinder;
|
import android.util.Log;
|
|
import androidx.annotation.Nullable;
|
|
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://xxx"; //服务器地址
|
private String userName = "xxx"; //账号
|
private String passWord = " xxx"; //密码
|
private static String myTopic = "topic"; //频道名
|
private String clientId = "mqtt_client"; //客户端ID
|
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
init();
|
return super.onStartCommand(intent, flags, startId);
|
}
|
|
public static void publish(String msg) {
|
String topic = myTopic;
|
Integer qos = 2;
|
Boolean retained = false;
|
try {
|
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 ="";
|
String topic = myTopic;
|
Integer qos = 2;
|
Boolean retained = false;
|
if ((!message.equals("")) || (!topic.equals(""))) {
|
// 最后
|
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() {
|
try {
|
client.disconnect(); //服务销毁,断开连接
|
} catch (MqttException e) {
|
e.printStackTrace();
|
}
|
super.onDestroy();
|
}
|
|
/**
|
* 连接MQTT服务器
|
*/
|
private void doClientConnection() {
|
if (!client.isConnected() && isConnectIsNomarl()) {
|
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());
|
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 isConnectIsNomarl() {
|
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;
|
}
|
}
|
|
@Nullable
|
@Override
|
public IBinder onBind(Intent intent) {
|
return null;
|
}
|
|
}
|