From 9e0852d6974cd7ed74bdb788074aab683fce437d Mon Sep 17 00:00:00 2001 From: MT <3075067877@qq.com> Date: Tue, 16 Sep 2025 16:25:58 +0800 Subject: [PATCH] fix 2 --- .../java/com/ouxuan/oxface/device/MqttManager.java | 43 +++++++++++----------- .../oxface/device/MqttManagerUsageExample.java | 4 +- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/ouxuan/oxface/device/MqttManager.java b/app/src/main/java/com/ouxuan/oxface/device/MqttManager.java index 517b299..aa2ec4e 100644 --- a/app/src/main/java/com/ouxuan/oxface/device/MqttManager.java +++ b/app/src/main/java/com/ouxuan/oxface/device/MqttManager.java @@ -11,6 +11,7 @@ import com.tencent.iot.hub.device.android.core.util.TXLog; import com.tencent.iot.hub.device.java.core.log.TXMqttLogCallBack; import com.tencent.iot.hub.device.java.core.mqtt.TXMqttActionCallBack; import com.tencent.iot.hub.device.java.core.mqtt.TXMqttConstants; +import com.tencent.iot.hub.device.java.core.common.Status; import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions; import org.eclipse.paho.client.mqttv3.IMqttToken; @@ -58,7 +59,7 @@ public class MqttManager { 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 int QOS = TXMqttConstants.QOS1; // 消息质量 + private static final int QOS = 1; // 消息质量 // 重连配置 private static final int MAX_RECONNECT_ATTEMPTS = 5; // 最大重连次数 @@ -359,13 +360,8 @@ public class MqttManager { */ private void checkConnectionHealth() { if (mqttConnection != null) { - boolean clientConnected = mqttConnection.isConnected(); - - if (isConnected && !clientConnected) { - LogManager.logWarning(TAG, "检测到连接状态不一致,触发重连"); - isConnected = false; - connectAsync(); - } else if (!isConnected && !isConnecting) { + // 简单检查连接状态 + if (!isConnected && !isConnecting) { LogManager.logInfo(TAG, "健康检查:连接已断开,尝试重连"); connectAsync(); } @@ -381,8 +377,8 @@ public class MqttManager { private class MqttActionCallback extends TXMqttActionCallBack { @Override - public void onConnectCompleted(int status, boolean reconnect, Object userContext, String msg) { - if (status == TXMqttConstants.MQTT_SDK_SUCCESS) { + public void onConnectCompleted(Status status, boolean reconnect, Object userContext, String msg) { + if (status == Status.OK) { LogManager.logInfo(TAG, "MQTT连接成功 - 重连: " + reconnect); isConnected = true; isConnecting = false; @@ -425,33 +421,33 @@ public class MqttManager { } @Override - public void onDisconnectCompleted(int status, Object userContext, String msg) { + public void onDisconnectCompleted(Status status, Object userContext, String msg, Throwable cause) { LogManager.logInfo(TAG, "MQTT连接已断开: " + msg); isConnected = false; isConnecting = false; } @Override - public void onPublishCompleted(int status, IMqttToken token, Object userContext, String msg) { - if (status == TXMqttConstants.MQTT_SDK_SUCCESS) { + public void onPublishCompleted(Status status, IMqttToken token, Object userContext, String errMsg) { + if (status == Status.OK) { LogManager.logDebug(TAG, "消息发布成功"); } else { - LogManager.logError(TAG, "消息发布失败: " + msg); + LogManager.logError(TAG, "消息发布失败: " + errMsg); } } @Override - public void onSubscribeCompleted(int status, IMqttToken token, Object userContext, String msg) { - if (status == TXMqttConstants.MQTT_SDK_SUCCESS) { + public void onSubscribeCompleted(Status status, IMqttToken token, Object userContext, String errMsg) { + if (status == Status.OK) { LogManager.logInfo(TAG, "主题订阅成功: " + subscribeTopic); } else { - LogManager.logError(TAG, "主题订阅失败: " + msg); + LogManager.logError(TAG, "主题订阅失败: " + errMsg); } } @Override - public void onUnSubscribeCompleted(int status, IMqttToken token, Object userContext, String msg) { - LogManager.logInfo(TAG, "取消订阅完成: " + msg); + public void onUnSubscribeCompleted(Status status, IMqttToken token, Object userContext, String errMsg) { + LogManager.logInfo(TAG, "取消订阅完成: " + errMsg); } @Override @@ -490,6 +486,11 @@ public class MqttManager { public boolean uploadLogFile() { return false; // 不上传日志文件 } + + @Override + public boolean delOfflineLog() { + return false; // 不删除离线日志 + } } /** @@ -678,7 +679,7 @@ public class MqttManager { * 获取连接状态 */ public boolean isConnected() { - return isConnected && mqttConnection != null && mqttConnection.isConnected(); + return isConnected && mqttConnection != null; } /** @@ -719,7 +720,7 @@ public class MqttManager { isConnected = false; isConnecting = false; - if (mqttConnection != null && mqttConnection.isConnected()) { + if (mqttConnection != null) { try { MQTTRequest mqttRequest = new MQTTRequest("disconnect", requestID.getAndIncrement()); mqttConnection.disConnect(mqttRequest); diff --git a/app/src/main/java/com/ouxuan/oxface/device/MqttManagerUsageExample.java b/app/src/main/java/com/ouxuan/oxface/device/MqttManagerUsageExample.java index 27fb448..71d2bda 100644 --- a/app/src/main/java/com/ouxuan/oxface/device/MqttManagerUsageExample.java +++ b/app/src/main/java/com/ouxuan/oxface/device/MqttManagerUsageExample.java @@ -191,8 +191,8 @@ public class MqttManagerUsageExample { // 4. 发送测试消息 sendTestMessage(); - // 5. 上报设备信息 - uploadDeviceInfo(); + // 5. 上报设备信息(已集成到MqttManager中) + // 在MqttManager中会自动处理设备信息上报 } }, 5000); // 延迟5秒等待连接建立