-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidMqttService.java
189 lines (165 loc) · 6.75 KB
/
AndroidMqttService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.homwee.gcpclient.mqtt;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import com.homwee.gcpclient.GCPClentApplication;
import com.homwee.gcpclient.JWTUtil;
import com.homwee.gcpclient.Logutil;
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 AndroidMqttService extends Service {
private static MqttAndroidClient mqttAndroidClient;
private MqttConnectOptions mMqttConnectOptions;
public String HOST = "ssl://mqtt.googleapis.com:8883";//服务器地址(协议+地址+端口号)
public String USERNAME = "unused";//用户名
public String PASSWORD = "password";//密码
public static String PUBLISH_TOPIC = "/devices/customerTV/commands/#";//发布主题
public static String RESPONSE_TOPIC = "/devices/customerTV/commands/#";//响应主题
public String CLIENTID = "projects/customer-gcp-001/locations/europe-west1/registries/customer-registry/devices/customerTV";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
init();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* 开启服务
*/
public static void startService(Context mContext) {
mContext.startService(new Intent(mContext, AndroidMqttService.class));
}
/**
* 发布 (模拟其他客户端发布消息)
*
* @param message 消息
*/
public static void publish(String message) {
String topic = PUBLISH_TOPIC;
Integer qos = 2;
Boolean retained = false;
try {
//参数分别为:主题、消息的字节数组、服务质量、是否在服务器保留断开连接后的最后一条消息
mqttAndroidClient.publish(topic, message.getBytes(), qos.intValue(), retained.booleanValue());
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 响应 (收到其他客户端的消息后,响应给对方告知消息已到达或者消息有问题等)
*
* @param message 消息
*/
public void response(String message) {
String topic = RESPONSE_TOPIC;
Integer qos = 2;
Boolean retained = false;
try {
//参数分别为:主题、消息的字节数组、服务质量、是否在服务器保留断开连接后的最后一条消息
mqttAndroidClient.publish(topic, message.getBytes(), qos.intValue(), retained.booleanValue());
} catch (MqttException e) {
e.printStackTrace();
}
}
/**
* 初始化
*/
private void init() {
mqttAndroidClient = new MqttAndroidClient(this, HOST, CLIENTID);
mqttAndroidClient.setCallback(mqttCallback); //设置监听订阅消息的回调
mMqttConnectOptions = new MqttConnectOptions();
mMqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
mMqttConnectOptions.setCleanSession(true); //设置是否清除缓存
mMqttConnectOptions.setConnectionTimeout(10); //设置超时时间,单位:秒
mMqttConnectOptions.setKeepAliveInterval(20); //设置心跳包发送间隔,单位:秒
mMqttConnectOptions.setUserName(USERNAME); //设置用户名
mMqttConnectOptions.setPassword(JWTUtil.createJwtRsa(GCPClentApplication.PROJECT_ID).toCharArray()); //设置密码
// last will message
// boolean doConnect = true;
// String message = "{\"terminal_uid\":\"" + CLIENTID + "\"}";
// String topic = PUBLISH_TOPIC;
// Integer qos = 2;
// Boolean retained = false;
// if ((!message.equals("")) || (!topic.equals(""))) {
// // 最后的遗嘱
// try {
// mMqttConnectOptions.setWill(topic, message.getBytes(), qos.intValue(), retained.booleanValue());
// } catch (Exception e) {
// e.printStackTrace();
// doConnect = false;
// iMqttActionListener.onFailure(null, e);
// }
// }
// if (doConnect) {
doClientConnection();
// }
}
/**
* 连接MQTT服务器
*/
private void doClientConnection() {
if (!mqttAndroidClient.isConnected()) {
try {
mqttAndroidClient.connect(mMqttConnectOptions, null, iMqttActionListener);
} catch (MqttException e) {
e.printStackTrace();
}
}
}
//MQTT是否连接成功的监听
private IMqttActionListener iMqttActionListener = new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken arg0) {
Logutil.i("connect MQTT success ");
try {
IMqttToken subscribe = mqttAndroidClient.subscribe(PUBLISH_TOPIC, 1);
Logutil.i(subscribe.getTopics()[0]);
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
arg1.printStackTrace();
Logutil.i("connect MQTT fail ");
}
};
//订阅主题的回调
private MqttCallback mqttCallback = new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Logutil.i("收到消息: " + new String(message.getPayload()));
//收到消息,这里弹出Toast表示。如果需要更新UI,可以使用广播或者EventBus进行发送
Toast.makeText(getApplicationContext(), "messageArrived: " + new String(message.getPayload()), Toast.LENGTH_LONG).show();
//收到其他客户端的消息后,响应给对方告知消息已到达或者消息有问题等
response("message arrived");
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
}
@Override
public void connectionLost(Throwable arg0) {
Logutil.i("连接断开 ");
doClientConnection();//连接断开,重连
}
};
@Override
public void onDestroy() {
try {
mqttAndroidClient.disconnect(); //断开连接
} catch (MqttException e) {
e.printStackTrace();
}
super.onDestroy();
}
}