JLChen
2020-09-24 e91af284643d5e370b0d18c384fe8de65f59d9b3
Android_HDL_SDK_XW/hdl_core/src/main/java/com/hdl/sdk/hdl_core/HDLDeviceManger/Core/HDLZigbeeMqtt.java
@@ -9,6 +9,7 @@
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Callback.PushCallback;
import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog;
@@ -17,7 +18,8 @@
    private static final String clientid = "HDLZigbeeDriver";
    private static MqttClient client;
    private static boolean isCancel  = false;
    private static boolean isCancel = false;
    public static void setIsCancel(boolean isCancel) {
        HDLZigbeeMqtt.isCancel = isCancel;
    }
@@ -28,25 +30,25 @@
     */
    public static void connect1(String host) {
        try {
                    client = new MqttClient("tcp://"+host+":1883", clientid, new MemoryPersistence());
                    MqttConnectOptions options = new MqttConnectOptions();
                    options.setCleanSession(false);
                    options.setUserName("admin");
                    options.setPassword("password".toCharArray());
                    //设置自动重连
                    options.setAutomaticReconnect(true);
                    // 设置超时时间
                    options.setConnectionTimeout(10);
                    // 设置会话心跳时间
                    options.setKeepAliveInterval(20);
                    PushCallback pushCallback = new PushCallback();
                    pushCallback.setHost(host);
                    client.setCallback(pushCallback);
                    client.connect(options);
                    if (isCancel){
                        client.close();
                        isCancel = false;
                    }
            client = new MqttClient("tcp://" + host + ":1883", clientid, new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(false);
            options.setUserName("admin");
            options.setPassword("password".toCharArray());
            //设置自动重连
            options.setAutomaticReconnect(true);
            // 设置超时时间
            options.setConnectionTimeout(10);
            // 设置会话心跳时间
            options.setKeepAliveInterval(20);
            PushCallback pushCallback = new PushCallback();
            pushCallback.setHost(host);
            client.setCallback(pushCallback);
            client.connect(options);
            if (isCancel) {
                client.close();
                isCancel = false;
            }
        } catch (MqttException e) {
            e.printStackTrace();
        }
@@ -55,9 +57,9 @@
    /**
     * 发送mqtt信息
     */
    public static void sendMqttData1(String topic, String json)  {
        HDLLog.info("send Mqtt to gateway:"+json);
        Log.d("111133", "sendMqttData1: "+json);
    public static void sendMqttData1(String topic, String json) {
        HDLLog.info("send Mqtt to gateway:" + json);
        Log.d("111133", "sendMqttData1: " + json);
        try {
            MqttMessage message = new MqttMessage();
            message.setQos(2);
@@ -68,7 +70,7 @@
            Log.d("111133", "已经发送");
            HDLLog.info("message is published completely!");
        } catch (MqttException e) {
            Log.d("111133", "sendMqttData1: "+e.getMessage());
            Log.d("111133", "sendMqttData1: " + e.getMessage());
            HDLLog.info("发送mqtt信息出错");
            e.printStackTrace();
        }