|
@ -54,9 +54,9 @@ public class MqttManager { |
|
|
private static final String PRODUCT_KEY = "qr3rximCZnT6ZU0NsAAiTC7O"; |
|
|
private static final String PRODUCT_KEY = "qr3rximCZnT6ZU0NsAAiTC7O"; |
|
|
|
|
|
|
|
|
// MQTT连接配置 |
|
|
// MQTT连接配置 |
|
|
private static final String BROKER_URL = null; // 传入null,使用腾讯云IoT默认地址 |
|
|
|
|
|
private static final int KEEP_ALIVE_INTERVAL = 120; |
|
|
|
|
|
private static final int CONNECTION_TIMEOUT = 10; |
|
|
|
|
|
|
|
|
private static final String BROKER_URL = "ssl://" + PRODUCT_ID + ".iotcloud.tencentdevices.com:8883"; // 腾讯云IoT Hub SSL地址 |
|
|
|
|
|
private static final int KEEP_ALIVE_INTERVAL = 240; // 增加心跳间隔 |
|
|
|
|
|
private static final int CONNECTION_TIMEOUT = 30; // 增加连接超时时间 |
|
|
private static final int QOS = 1; |
|
|
private static final int QOS = 1; |
|
|
|
|
|
|
|
|
// 重连配置 |
|
|
// 重连配置 |
|
@ -216,6 +216,9 @@ public class MqttManager { |
|
|
*/ |
|
|
*/ |
|
|
private void connectMqtt() { |
|
|
private void connectMqtt() { |
|
|
LogManager.logInfo(TAG, "开始连接MQTT服务器..."); |
|
|
LogManager.logInfo(TAG, "开始连接MQTT服务器..."); |
|
|
|
|
|
LogManager.logInfo(TAG, "连接地址: " + BROKER_URL); |
|
|
|
|
|
LogManager.logInfo(TAG, "产品ID: " + PRODUCT_ID); |
|
|
|
|
|
LogManager.logInfo(TAG, "设备名称: " + deviceName); |
|
|
isConnecting = true; |
|
|
isConnecting = true; |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
@ -228,8 +231,8 @@ public class MqttManager { |
|
|
DEV_PSK, |
|
|
DEV_PSK, |
|
|
null, // devCert |
|
|
null, // devCert |
|
|
null, // devPriv |
|
|
null, // devPriv |
|
|
false, // mqttLogFlag |
|
|
|
|
|
null, // logCallBack |
|
|
|
|
|
|
|
|
true, // 启用MQTT日志以便调试 |
|
|
|
|
|
new MqttLogCallback(), // 添加日志回调 |
|
|
new MqttActionCallback() |
|
|
new MqttActionCallback() |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
@ -238,9 +241,12 @@ public class MqttManager { |
|
|
options.setConnectionTimeout(CONNECTION_TIMEOUT); |
|
|
options.setConnectionTimeout(CONNECTION_TIMEOUT); |
|
|
options.setKeepAliveInterval(KEEP_ALIVE_INTERVAL); |
|
|
options.setKeepAliveInterval(KEEP_ALIVE_INTERVAL); |
|
|
options.setAutomaticReconnect(false); // 手动控制重连 |
|
|
options.setAutomaticReconnect(false); // 手动控制重连 |
|
|
options.setCleanSession(false); |
|
|
|
|
|
|
|
|
options.setCleanSession(true); // 改为true,避免会话问题 |
|
|
|
|
|
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); // 明确指定MQTT版本 |
|
|
|
|
|
|
|
|
LogManager.logInfo(TAG, "使用PSK认证连接腾讯云IoT平台"); |
|
|
LogManager.logInfo(TAG, "使用PSK认证连接腾讯云IoT平台"); |
|
|
|
|
|
LogManager.logInfo(TAG, "连接超时: " + CONNECTION_TIMEOUT + "秒"); |
|
|
|
|
|
LogManager.logInfo(TAG, "心跳间隔: " + KEEP_ALIVE_INTERVAL + "秒"); |
|
|
|
|
|
|
|
|
// 连接MQTT |
|
|
// 连接MQTT |
|
|
MQTTRequest mqttRequest = new MQTTRequest("connect", requestID.getAndIncrement()); |
|
|
MQTTRequest mqttRequest = new MQTTRequest("connect", requestID.getAndIncrement()); |
|
@ -293,13 +299,16 @@ public class MqttManager { |
|
|
private void scheduleReconnect() { |
|
|
private void scheduleReconnect() { |
|
|
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) { |
|
|
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) { |
|
|
LogManager.logError(TAG, "已达到最大重连次数,停止重连"); |
|
|
LogManager.logError(TAG, "已达到最大重连次数,停止重连"); |
|
|
|
|
|
LogManager.logError(TAG, "请检查网络连接和腾讯云IoT设备配置"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
reconnectAttempts++; |
|
|
reconnectAttempts++; |
|
|
long delay = RECONNECT_DELAY * reconnectAttempts; |
|
|
|
|
|
|
|
|
// 渐进式延迟:5s, 10s, 20s, 40s, 80s |
|
|
|
|
|
long delay = RECONNECT_DELAY * (1L << (reconnectAttempts - 1)); |
|
|
|
|
|
|
|
|
LogManager.logInfo(TAG, "调度第" + reconnectAttempts + "次重连,延迟" + delay + "ms"); |
|
|
LogManager.logInfo(TAG, "调度第" + reconnectAttempts + "次重连,延迟" + delay + "ms"); |
|
|
|
|
|
LogManager.logInfo(TAG, "重连原因: 代理程序不可用,可能是网络问题或认证失败"); |
|
|
|
|
|
|
|
|
if (connectionStatusListener != null) { |
|
|
if (connectionStatusListener != null) { |
|
|
mainHandler.post(() -> connectionStatusListener.onReconnecting(reconnectAttempts)); |
|
|
mainHandler.post(() -> connectionStatusListener.onReconnecting(reconnectAttempts)); |
|
@ -308,6 +317,14 @@ public class MqttManager { |
|
|
scheduledExecutor.schedule(() -> { |
|
|
scheduledExecutor.schedule(() -> { |
|
|
if (!isConnected) { |
|
|
if (!isConnected) { |
|
|
LogManager.logInfo(TAG, "执行第" + reconnectAttempts + "次重连"); |
|
|
LogManager.logInfo(TAG, "执行第" + reconnectAttempts + "次重连"); |
|
|
|
|
|
// 清理旧连接 |
|
|
|
|
|
if (mqttConnection != null) { |
|
|
|
|
|
try { |
|
|
|
|
|
mqttConnection = null; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
LogManager.logError(TAG, "清理旧连接失败", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
connectAsync(); |
|
|
connectAsync(); |
|
|
} |
|
|
} |
|
|
}, delay, TimeUnit.MILLISECONDS); |
|
|
}, delay, TimeUnit.MILLISECONDS); |
|
@ -345,6 +362,36 @@ public class MqttManager { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
* MQTT日志回调处理器 |
|
|
|
|
|
*/ |
|
|
|
|
|
private class MqttLogCallback extends TXMqttLogCallBack { |
|
|
|
|
|
@Override |
|
|
|
|
|
public String setSecretKey() { |
|
|
|
|
|
return null; // 不需要加密 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void printDebug(String message) { |
|
|
|
|
|
LogManager.logDebug("TXMQTT", message); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void printInfo(String message) { |
|
|
|
|
|
LogManager.logInfo("TXMQTT", message); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void printWarn(String message) { |
|
|
|
|
|
LogManager.logWarning("TXMQTT", message); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void printError(String message) { |
|
|
|
|
|
LogManager.logError("TXMQTT", message); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
* MQTT动作回调处理器 |
|
|
* MQTT动作回调处理器 |
|
|
*/ |
|
|
*/ |
|
|
private class MqttActionCallback extends TXMqttActionCallBack { |
|
|
private class MqttActionCallback extends TXMqttActionCallBack { |
|
@ -359,6 +406,10 @@ public class MqttManager { |
|
|
status.name(), reconnect, userContextInfo, msg); |
|
|
status.name(), reconnect, userContextInfo, msg); |
|
|
LogManager.logInfo(TAG, "连接回调:" + logInfo); |
|
|
LogManager.logInfo(TAG, "连接回调:" + logInfo); |
|
|
|
|
|
|
|
|
|
|
|
if (cause != null) { |
|
|
|
|
|
LogManager.logError(TAG, "连接异常信息", cause); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (status.equals(Status.OK)) { |
|
|
if (status.equals(Status.OK)) { |
|
|
LogManager.logInfo(TAG, "MQTT连接成功"); |
|
|
LogManager.logInfo(TAG, "MQTT连接成功"); |
|
|
isConnected = true; |
|
|
isConnected = true; |
|
@ -376,12 +427,17 @@ public class MqttManager { |
|
|
mainHandler.post(() -> connectionStatusListener.onConnected()); |
|
|
mainHandler.post(() -> connectionStatusListener.onConnected()); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
LogManager.logError(TAG, "MQTT连接失败: " + msg); |
|
|
|
|
|
|
|
|
String errorMsg = "连接失败: " + msg; |
|
|
|
|
|
if (msg != null && msg.contains("代理程序不可用")) { |
|
|
|
|
|
errorMsg += "\n请检查:\n1. 网络连接是否正常\n2. 腾讯云IoT设备信息是否正确\n3. PSK密钥是否有效"; |
|
|
|
|
|
} |
|
|
|
|
|
LogManager.logError(TAG, "MQTT" + errorMsg); |
|
|
isConnecting = false; |
|
|
isConnecting = false; |
|
|
|
|
|
|
|
|
// 通知连接失败 |
|
|
// 通知连接失败 |
|
|
if (connectionStatusListener != null) { |
|
|
if (connectionStatusListener != null) { |
|
|
mainHandler.post(() -> connectionStatusListener.onConnectionFailed(msg)); |
|
|
|
|
|
|
|
|
final String finalErrorMsg = errorMsg; |
|
|
|
|
|
mainHandler.post(() -> connectionStatusListener.onConnectionFailed(finalErrorMsg)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 调度重连 |
|
|
// 调度重连 |
|
|