hxb
2022-09-08 2a01ef5e49422cca49bc7476fc1b8be8c8556561
HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -3,14 +3,13 @@
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
@@ -21,45 +20,33 @@
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlsdk.device.DevicesListActivity;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.event.EventListener;
import com.hdl.sdk.common.exception.HDLLinkException;
import com.hdl.sdk.common.utils.IdUtils;
import com.hdl.sdk.common.utils.IpUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.HDLLink;
import com.hdl.sdk.connect.bean.LinkRequest;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.bean.request.AuthenticateRequest;
import com.hdl.sdk.connect.bean.request.PropertyReadRequest;
import com.hdl.sdk.connect.bean.response.BaseLocalResponse;
import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
import com.hdl.sdk.connect.callback.HDLLinkCallBack;
import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.connect.socket.HDLAuthSocket;
import com.hdl.sdk.connect.socket.HDLSocket;
import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
import com.hdl.sdk.connect.protocol.LinkMessageDecoder;
import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
import com.hdl.sdk.socket.SocketOptions;
import com.hdl.sdk.socket.codec.MessagePipeLine;
import java.util.Base64;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.bean.LinkFunctionBean;
import com.hdl.sdk.link.bean.LinkSceneBean;
import com.hdl.sdk.link.common.config.TopicConstant;
import com.hdl.sdk.link.common.event.EventListener;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.common.utils.IdUtils;
import com.hdl.sdk.link.common.utils.gson.GsonConvert;
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.request.AuthenticateRequest;
import com.hdl.sdk.link.core.bean.request.DeviceControlRequest;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.bean.response.GatewaySearchBean;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkResponseCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkTCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.core.connect.HDLUdpConnect;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH_REPLY;
import static java.util.Base64.*;
public class MainActivity extends AppCompatActivity {
@@ -70,32 +57,39 @@
    private TextView responseTv;
    boolean isOn;
    private EventListener allTopicsListener;
    private String testLightSid = "0001010D48C71B02020100010101";
    private String testLightSid = "1493092497588469761";
    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllTopicsListener();
    }
void init() {
//    HDLLinkConfig.getInstance().setLocalSecret("7d04c4e3c2b7d600");
//    HDLLinkConfig.getInstance().setGatewayId("1473119283609321473");
//    HDLLinkConfig.getInstance().setLocalEncrypt(true);
}
    /**
     * 初始化HDLSDK
     */
    void initHDLSDK() {
        HDLLinkLocalSdk.getInstance().init("1530018255883706369","735c1677504a7034");
    }
    /**
     * 三方设备集成到HDL时,需要初始化的信息
     */
    void initThird(){
        initDeviceInfo();//初始化基本信息,非常重要,认证时要用
        checkIfCertified();//检测是否已经认证了,用于判断
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        responseTv = findViewById(R.id.response_tv);
        tv = findViewById(R.id.state_tv);
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
         init();
        checkIfCertified();
        initDeviceInfo();
        registerAllTopicsListener();
        initView();
        initHDLSDK();//这步比较重要,在使用前要设置好当前网关Id及加解密钥
//        initThird();//三方设备集成时要初始化
        registerAllTopicsListener();//注册监听接收所有主题的数据
        ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
@@ -103,7 +97,7 @@
            }
        });
        //初始化安卓权限
        launcher.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE});
        final List<DemoBean> beans = new ArrayList<>();
@@ -124,15 +118,9 @@
        rv.setAdapter(demoAdapter);
//        final SocketOptions options = new SocketOptions();
//
//        MessagePipeLine pipeLine = new MessagePipeLine();
//        options.setHandleMessage(pipeLine);
//        options.setEnabledHeartbeat(false);
        demoAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
            public void onItemClick(BaseQuickAdapter<?, ?> adapter, View view, int position) {
                switch (position) {
                    case 0:
                        //入网认证
@@ -168,7 +156,7 @@
                        break;
                    case 8:
                        //功能列表
                        startDevicesListActivity();
//                        startDevicesListActivity();
                        break;
                    case 9:
                        //UDP发送
@@ -190,6 +178,13 @@
            }
        });
    }
    //初始化控件
    void initView(){
        responseTv = findViewById(R.id.response_tv);
        tv = findViewById(R.id.state_tv);
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
    }
    public void showToast(String text) {
@@ -200,7 +195,7 @@
     * 检测是否认证过
     */
    void checkIfCertified() {
        boolean isCertified = HDLLink.getInstance().checkIfCertified();
        boolean isCertified = HDLLinkLocalSdk.getInstance().checkIfCertified();
        String mes = isCertified ? "已经认证过" : "未认证";
        showToast(mes);
        tv.setText(mes);
@@ -217,7 +212,7 @@
                handleLinkResponse(response);
            }
        };
        HDLLink.getInstance().registerAllTopicsListener(allTopicsListener);
        HDLLinkLocalSdk.getInstance().registerAllTopicsListener(allTopicsListener);
    }
    /**
@@ -238,7 +233,6 @@
            }
        } else {
            //其它主题
        }
    }
@@ -260,25 +254,26 @@
     * 移除所有主题数据的监听
     */
    void removeAllTopicsListener() {
        HDLLink.getInstance().removeAllTopicsListener(allTopicsListener);
        HDLLinkLocalSdk.getInstance().removeAllTopicsListener(allTopicsListener);
    }
    void initDeviceInfo()
    {
    void initDeviceInfo() {
        AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean();
        infoBean.setDeviceMAC("AA000000000000AF");
        infoBean.setIPMAC("AA000000000000AF");
        infoBean.setDeviceMAC("AA00000000000100");
        infoBean.setIPMAC("AA00000000000100");
        infoBean.setDeviceName("音乐播放器");//设备名字
        infoBean.setDeviceModel("MCLog.431");//设备型号
        infoBean.setAccessMode("WIFI");
        infoBean.setIPGateway("192.168.1.1");
        infoBean.setIPAddress("192.168.1.101");
        infoBean.setIPAddress("192.168.1.116");
        infoBean.setGateway_type("music.standard");
        infoBean.setHw_version("HW2.0");
        infoBean.setFw_version("Fw1.0");
        infoBean.setOID("010105000000FE10");//每个设备oid都要不一样
        infoBean.setSid("110105000000FE08110100000000");//每个设备的sid都要不一样
        infoBean.setOID("010105000000FE11");//每个设备oid都要不一样
        infoBean.setSid("110105000000FE08110100000011");//每个设备的sid都要不一样
        HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean);
    }
    /**
@@ -288,15 +283,15 @@
        tv.setText("开始入网认证...");
        //认证提交参数准备
        //测试服务
        String spkStr = "ir.module";//产品spk
        String macStr = "AA000000000000AF";//设备唯一MAC地址
        String secret = "44b360eb74b7ba64";//通过spk和mac提交云端认证后分配的secret
//        测试服务
//        String spkStr = "ir.module";//产品spk
//        String macStr = "AA000000000000AF";//设备唯一MAC地址
//        String secret = "44b360eb74b7ba64";//通过spk和mac提交云端认证后分配的secret
//        //正式服务器
//        String spkStr = "screen.mirror";//产品spk
//        String macStr = "AA000000000000AC";//设备唯一MAC地址
//        String secret = "ee62124c151b737c";//通过spk和mac提交云端认证后分配的secret
//        正式服务器
        String spkStr = "screen.mirror";//产品spk
        String macStr = "AA00000000000100";//设备唯一MAC地址
        String secret = "e186beeb7974998e";//通过spk和mac提交云端认证后分配的secret
        String mac_key = stringToMD5(stringToMD5(macStr + secret));
        String versionString = "HDL_V1.0.1";//
@@ -315,18 +310,19 @@
        authbean.setRequest(requestBean);
        //HDLLinkConfig.getInstance().getDeviceInfoBean()这个初始化的时候要先设置好
        AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean);
        HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() {
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("认证失败");
                responseTv.setText(e.getMsg());
                Log.i("TAG", "onError: 认证失败");
//                tv.setText("认证失败");
//                responseTv.setText(e.getMsg());
                Log.i("TAG", "onError: 认证失败" + e.getMsg());
            }
            @Override
            public void onSuccess(String msg) {
                tv.setText("认证成功");
                responseTv.setText(msg.toString());
//                tv.setText("认证成功");
//                responseTv.setText(msg.toString());
                Log.i("TAG", "onSuccess: 认证成功" + msg.toString());
            }
        });
    }
@@ -359,18 +355,14 @@
     */
    void searchGatewayBroadcast() {
        tv.setText("搜索网关中...");
        HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() {
        HDLLinkLocalSdk.getInstance().searchGatewayBroadcast(new HDLUdpConnect.SearchGatewayCallBack() {
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("网关不在线");
                responseTv.setText(e.getMsg());
            public void onSuccess(GatewaySearchBean gatewaySearchBean) {
            }
            @Override
            public void onSuccess(GatewaySearchBean gatewaySearchBean) {
                tv.setText("网关在线");
                responseTv.setText("搜索成功 网关id:" + gatewaySearchBean.getGatewayId());
//                                LogUtils.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId());
            public void onError(HDLLinkException e) {
            }
        });
    }
@@ -381,16 +373,15 @@
    void getFunctionList() {
        tv.setText("获取功能列表中...");
        responseTv.setText("");
        HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().getFunctionList(new HDLLinkTCallBack<List<LinkFunctionBean>>() {
            @Override
            public void onError(HDLLinkException error) {
                tv.setText(error.getMsg());
            public void onSuccess(List<LinkFunctionBean> data) {
            }
            @Override
            public void onSuccess(String data) {
                tv.setText("获取功能列表成功");
                responseTv.setText(data);
            public void onError(HDLLinkException e) {
            }
        });
    }
@@ -403,15 +394,16 @@
        tv.setText("功能属性读取");
        List<String> sids = new ArrayList<>();
        sids.add(testLightSid);
        HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().getFunctionAttribute(sids, new HDLLinkTCallBack<List<LinkFunctionBean>>() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
            public void onSuccess(List<LinkFunctionBean> data) {
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
@@ -424,23 +416,21 @@
        tv.setText("读取状态中...");
        List<String> list = new ArrayList<>();
        list.add(testLightSid);//要读取设备的sid
        HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().propertyRead(list, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
                tv.setText("读取成功");
                responseTv.setText(data);
            public void onSuccess(String msg) {
            }
            @Override
            public void onError(HDLLinkException e) {
                tv.setText("读取失败");
                responseTv.setText(e.getMsg());
            }
        });
    }
    /**
     * 控制失败
     * 控制设备
     * 回复响应code为200 代表执行成功
     */
    void controlDecide() {
@@ -456,15 +446,17 @@
        statusBeanList.add(bean);
        request.setStatus(statusBeanList);
        requestList.add(request);
        HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().propertyDown(requestList, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String data) {
                responseTv.setText(data);
//                responseTv.setText(data);
                Log.i("TAG", "onSuccess 控制设备成功" + data.toString());
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
//                responseTv.setText(e.getMsg());
                Log.i("TAG", "onError 控制设备失败" + e.getMsg());
            }
        });
    }
@@ -474,15 +466,15 @@
     */
    void getSceneList() {
        tv.setText("读取场景列表");
        HDLLink.getInstance().getSceneList(new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().getSceneList(new HDLLinkTCallBack<List<LinkSceneBean>>() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
            public void onSuccess(List<LinkSceneBean> data) {
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
            }
        });
    }
@@ -504,24 +496,26 @@
        } else {
            sids.add("04010560D2C76E0A0A0100010000");
        }
        HDLLink.getInstance().controlScene(sids, new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().controlScene(sids, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                responseTv.setText(msg);
//                responseTv.setText(msg);
                Log.i("TAG", "onSuccess 场景控制成功" + msg.toString());
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
//                responseTv.setText(e.getMsg());
                Log.i("TAG", "onError 场景控制失败" + e.getMsg());
            }
        });
    }
    void startDevicesListActivity() {
/*    void startDevicesListActivity() {
        Intent intent = new Intent(this, DevicesListActivity.class);
        startActivity(intent);
    }
    }*/
    /**
     * TCP发送 只发一次,不监听回复,不重发
@@ -530,7 +524,7 @@
        tv.setText("TCP发送 只发一次,不监听回复,不重发");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr);
        HDLLinkLocalSdk.getInstance().tcpSendMsg(propertyDownTopic, bodyStr);
    }
    /**
@@ -540,7 +534,7 @@
        tv.setText("UDP发送 只发一次,不监听回复,不重发");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr);
        HDLLinkLocalSdk.getInstance().udpSendMsg(propertyDownTopic, bodyStr,null);
    }
    private String getPropertyDownBodyStr() {
@@ -572,16 +566,17 @@
        tv.setText("UDP发送,监听主题回复,带重发带回调");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() {
        HDLLinkLocalSdk.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() {
            @Override
            public void onSuccess(LinkResponse msg) {
                Log.i("udpSendWithCallback", "udpSendWithCallback");
                responseTv.setText(GsonConvert.getGson().toJson(msg));
                Log.i("TAG", "onSuccess UDP发送成功" + GsonConvert.getGson().toJson(msg));
//                responseTv.setText(GsonConvert.getGson().toJson(msg));
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
//                responseTv.setText(e.getMsg());
                Log.i("TAG", "onError UDP发送失败" + e.getMsg());
            }
        });
    }
@@ -593,16 +588,17 @@
        tv.setText("TCP发送,监听主题回复,带重发带回调");
        String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId());
        String bodyStr = getPropertyDownBodyStr();
        HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() {
        HDLLinkLocalSdk.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() {
            @Override
            public void onSuccess(String msg) {
                Log.i("tcpSendWithCallback", "tcpSendWithCallback");
                responseTv.setText(msg);
                Log.i("TAG", "onSuccess TCP发送成功" + msg);
//                responseTv.setText(msg);
            }
            @Override
            public void onError(HDLLinkException e) {
                responseTv.setText(e.getMsg());
//                responseTv.setText(e.getMsg());
                Log.i("TAG", "onError TCP发送失败");
            }
        });
    }