JLChen
2021-11-15 1323c0b3463c5486be9d5e5310782aeb9023697c
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/LinkRequest.java
@@ -1,18 +1,16 @@
package com.hdl.sdk.connect.bean;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import com.hdl.sdk.common.config.AuthenticateConfig;
import com.hdl.sdk.common.utils.ByteUtils;
import com.hdl.sdk.connect.utils.AESUtils;
import com.hdl.sdk.connect.utils.AesUtil;
import java.io.UnsupportedEncodingException;
import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH;
//import com.hdl.sdk.connect.utils.AesUtil;
/**
 * Created by Tong on 2021/9/29.
@@ -47,7 +45,6 @@
        this.data = data;
        if (!TextUtils.isEmpty(data)) {
            setLength(getBytesLength(data));
//            setLength(data.length());
        } else {
            setLength(0);
        }
@@ -74,32 +71,13 @@
                getData();
    }
    private static byte[] stringToBytes(String str) {
        try {
            // 使用指定的字符集将此字符串编码为byte序列并存到一个byte数组中
            return str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new byte[]{};
    }
    private int getBytesLength(String str){
        return stringToBytes(str).length;
        return ByteUtils.stringToBytes(str).length;
    }
    /**
     * 合并数组
     * @param bt1
     * @param bt2
     * @return
     */
    public static byte[] byteMerger(byte[] bt1, byte[] bt2){
        byte[] bt3 = new byte[bt1.length + bt2.length];
        System.arraycopy(bt1, 0, bt3, 0, bt1.length);
        System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
        return bt3;
    }
    /**
     * 获取发送数据byte
     *
@@ -110,11 +88,12 @@
            //判断是否需要加密
            if (AuthenticateConfig.getInstance().ifNeedEncrypt(topic)) {
                //需要加密
                byte[] dataBytes = AesUtil.aesEncrypt(stringToBytes(data), AuthenticateConfig.getInstance().getLocalSecret());
                byte[] dataBytes = AesUtil.aesEncrypt(ByteUtils.stringToBytes(data), AuthenticateConfig.getInstance().getLocalSecret());
//                byte[] dataBytes = AESUtils.encryptAES(stringToBytes(data), AuthenticateConfig.getInstance().getLocalSecret());
                String headString = "Topic:" + getTopic() + "\r\n" + "Length:" + dataBytes.length + "\r\n" + "\r\n";
//                byte[] headBytes = headString.getBytes("utf-8");
                byte[] headBytes = headString.getBytes();
                byte[] sendBytes = byteMerger(headBytes, dataBytes);
                byte[] headBytes = headString.getBytes("utf-8");
                byte[] sendBytes = ByteUtils.concatBytes(headBytes, dataBytes);
                return sendBytes;
            } else {
                return this.toString().getBytes("utf-8");