| | |
| | | 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; |
| | | import android.util.Log; |
| | |
| | | |
| | | 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.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 com.hdl.sdk.socket.SocketBoot; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.MessageDigest; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH_REPLY; |
| | | |
| | | public class MainActivity extends AppCompatActivity { |
| | | |
| | |
| | | private TextView responseTv; |
| | | boolean isOn; |
| | | private EventListener allTopicsListener; |
| | | private String testLightSid = "0001010D48C71B02020100010101"; |
| | | private String testLightSid = "1493092497588469761"; |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | |
| | | removeAllTopicsListener(); |
| | | } |
| | | |
| | | void init() { |
| | | HDLLinkConfig.getInstance().setLocalSecret("e186beeb7974998e"); |
| | | HDLLinkConfig.getInstance().setGatewayId("1519863101509361666"); |
| | | } |
| | | |
| | | @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(); |
| | | |
| | | ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { |
| | |
| | | break; |
| | | case 8: |
| | | //功能列表 |
| | | startDevicesListActivity(); |
| | | // startDevicesListActivity(); |
| | | break; |
| | | case 9: |
| | | //UDP发送 |
| | |
| | | String mes = isCertified ? "已经认证过" : "未认证"; |
| | | showToast(mes); |
| | | tv.setText(mes); |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public void onMessage(Object msg) { |
| | | LinkResponse response = (LinkResponse) msg; |
| | | handleLinkResponse(response); |
| | | } |
| | | }; |
| | | HDLLink.getInstance().registerAllTopicsListener(allTopicsListener); |
| | |
| | | HDLLink.getInstance().removeAllTopicsListener(allTopicsListener); |
| | | } |
| | | |
| | | void initDeviceInfo() { |
| | | AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); |
| | | 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.116"); |
| | | infoBean.setGateway_type("music.standard"); |
| | | infoBean.setHw_version("HW2.0"); |
| | | infoBean.setFw_version("Fw1.0"); |
| | | infoBean.setOID("010105000000FE11");//每个设备oid都要不一样 |
| | | infoBean.setSid("110105000000FE08110100000011");//每个设备的sid都要不一样 |
| | | HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean); |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 入网认证 |
| | | */ |
| | |
| | | tv.setText("开始入网认证..."); |
| | | //认证提交参数准备 |
| | | |
| | | //测试服务 |
| | | String spkStr = "ir.module";//产品spk |
| | | String macStr = "AA000000000000BB";//设备唯一MAC地址 |
| | | String secret = "87ae414b7a853f65";//通过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";// |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | HDLLinkConfig.getInstance().getDeviceInfoBean().setDeviceMAC(macStr); |
| | | |
| | | //1.设置认证信息 |
| | | AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean(); |
| | | requestBean.setMAC(macStr); |
| | | requestBean.setSupplier("raysgem"); |
| | | requestBean.setSupplier("WISE"); |
| | | requestBean.setFirmwareVersion(versionString); |
| | | requestBean.setHardwareModel("1956F"); |
| | | AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean(); |
| | | authbean.setSpk(spkStr); |
| | | authbean.setMACKey(mac_key); |
| | | authbean.setRequest(requestBean); |
| | | |
| | | //2.设置设备信息 |
| | | AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); |
| | | infoBean.setDeviceMAC(macStr); |
| | | infoBean.setIPMAC(macStr); |
| | | infoBean.setDeviceName("红外宝");//设备名字 |
| | | infoBean.setDeviceModel("HDL");// |
| | | infoBean.setAccessMode("WIFI"); |
| | | infoBean.setIPGateway("192.168.88.1"); |
| | | infoBean.setIPAddress(IpUtils.getIP(this)); |
| | | |
| | | infoBean.setOID("010105000000FE08"); |
| | | infoBean.setSid("010105000000FE08110100000000"); |
| | | // infoBean.set |
| | | AuthenticateRequest.VersionBean[] versionBeans = new AuthenticateRequest.VersionBean[]{new AuthenticateRequest.VersionBean("FW", versionString), new AuthenticateRequest.VersionBean("HW", "1956F")}; |
| | | infoBean.setVersions(versionBeans); |
| | | AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, infoBean, authbean); |
| | | //HDLLinkConfig.getInstance().getDeviceInfoBean()这个初始化的时候要先设置好 |
| | | AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean); |
| | | HDLLink.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()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | tv.setText("网关不在线"); |
| | | responseTv.setText(e.getMsg()); |
| | | // tv.setText("网关不在线"); |
| | | // responseTv.setText(e.getMsg()); |
| | | Log.i("TAG", "网关不在线" + e.getMsg()); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | tv.setText("网关在线"); |
| | | responseTv.setText("搜索成功 网关id:" + gatewaySearchBean.getGatewayId()); |
| | | // LogUtils.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | // tv.setText("网关在线"); |
| | | // responseTv.setText("搜索成功 网关id:" + gatewaySearchBean.getGatewayId()); |
| | | Log.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() { |
| | | @Override |
| | | public void onError(HDLLinkException error) { |
| | | tv.setText(error.getMsg()); |
| | | // tv.setText(error.getMsg()); |
| | | Log.i("TAG", "onError 获取功能列表失败" + error.getMsg()); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("获取功能列表成功"); |
| | | responseTv.setText(data); |
| | | // tv.setText("获取功能列表成功"); |
| | | // responseTv.setText(data); |
| | | Log.i("TAG", "onSuccess 获取功能列表成功" + data.toString()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.getInstance().getFunctionAttribute(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()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("读取成功"); |
| | | responseTv.setText(data); |
| | | // tv.setText("读取成功"); |
| | | // responseTv.setText(data); |
| | | Log.i("TAG", "onSuccess 读取设备状态成功" + data.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | tv.setText("读取失败"); |
| | | responseTv.setText(e.getMsg()); |
| | | // tv.setText("读取失败"); |
| | | // responseTv.setText(e.getMsg()); |
| | | Log.i("TAG", "onError 读取设备状态失败" + e.getMsg()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 控制失败 |
| | | * 控制设备 |
| | | * 回复响应code为200 代表执行成功 |
| | | */ |
| | | void controlDecide() { |
| | |
| | | HDLLink.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()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.getInstance().getSceneList(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()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.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发送 只发一次,不监听回复,不重发 |
| | |
| | | HDLLink.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()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | HDLLink.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发送失败"); |
| | | } |
| | | }); |
| | | } |