panlili2024
2025-02-21 d31f2155237aa65cee1cb6ce1d39c48191663226
HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -2,6 +2,7 @@
import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
@@ -14,6 +15,7 @@
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;
@@ -23,9 +25,11 @@
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.DeleteNetworkListener;
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.LogUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.HDLLink;
import com.hdl.sdk.connect.bean.LinkResponse;
@@ -48,6 +52,8 @@
import java.util.List;
import java.util.Map;
import retrofit2.http.HEAD;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
@@ -55,10 +61,11 @@
    private RecyclerView rv;
    private TextView tv;
    private TextView responseTv;
    private TextView localSecret;
    private EditText editText;
    boolean isOn;
    private EventListener allTopicsListener;
    private String testLightSid = "0001010D48C71B02020100010101";
    private String testLightSid = "02010131D9C03D01020300010107";
    private String secret = "";
    @Override
@@ -69,8 +76,10 @@
    void initDeviceInfo() {
        AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean();
        infoBean.setDeviceMAC("f2c5d8bad48f");
        infoBean.setIPMAC("f2c5d8bad48f");
        infoBean.setDeviceName("音乐播放器");//设备名字
        infoBean.setDeviceModel("MCLog.431");//设备型号
        infoBean.setAccessMode("WIFI");
@@ -144,7 +153,7 @@
        String spkStr = "screen.touch";//产品spk
//        String macStr = "AA00000000000100";//设备唯一MAC地址
        String macStr = editText.getText().toString();//设备唯一MAC地址
//        String secret = "e186beeb7974998e";//通过spk和mac提交云端认证后分配的secret
//        String secret = "e186beeb7974998e";//通过spk和mac提交云端认证后分配的
        String mac_key = stringToMD5(stringToMD5(macStr + secret));
        String versionString = "HDL_V1.0.1";//
@@ -164,6 +173,8 @@
        authbean.setSpk(spkStr);
        authbean.setMACKey(mac_key);
        authbean.setRequest(requestBean);
        HDLLinkConfig.getInstance().setAuthBean(authbean);
        //HDLLinkConfig.getInstance().getDeviceInfoBean()这个初始化的时候要先设置好
        AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean);
        HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() {
@@ -174,10 +185,12 @@
                Log.e(TAG, "onError: 认证失败 " + e.getCode());
            }
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onSuccess(String msg) {
                tv.setText("认证成功");
                responseTv.setText(msg.toString());
                localSecret.setText("密钥:" + HDLLinkConfig.getInstance().getLocalSecret());
            }
        });
    }
@@ -207,6 +220,7 @@
        setContentView(R.layout.activity_main);
        responseTv = findViewById(R.id.response_tv);
        editText = findViewById(R.id.edittext);
        localSecret = findViewById(R.id.local_secret);
        tv = findViewById(R.id.state_tv);
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
@@ -215,6 +229,17 @@
        checkIfCertified();
        initDeviceInfo();//初始化
        registerAllTopicsListener();
        HDLLink.getInstance().setDeleteNetworkListener(new DeleteNetworkListener() {
            @Override
            public void onSuccess(Object msg) {
                LogUtils.i("setDeleteNetworkListener onSucceed = " + msg);
            }
            @Override
            public void onFailure() {
                LogUtils.i("setDeleteNetworkListener onFailure");
            }
        });
        ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
@@ -352,6 +377,9 @@
            @Override
            public void onMessage(Object msg) {
                LinkResponse response = (LinkResponse) msg;
                if ("/user/all/custom/gateway/broadcast_reply".equals(response.getTopic())){
                    localSecret.setText("密钥:" + HDLLinkConfig.getInstance().getLocalSecret());
                }
            }
        };
        HDLLink.getInstance().registerAllTopicsListener(allTopicsListener);