package com.hdl.sdk.hdl_sdk.activity;
|
|
import android.support.v7.app.AppCompatActivity;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.view.View;
|
import android.widget.Button;
|
|
import com.hdl.sdk.hdl_core.Config.Configuration;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeGatewayInfoBean;
|
import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog;
|
import com.hdl.sdk.hdl_core.Util.NetUtil.NetWorkUtil;
|
import com.hdl.sdk.hdl_core.Util.TransformUtil.StringUtil;
|
import com.hdl.sdk.hdl_sdk.R;
|
|
import java.net.DatagramPacket;
|
import java.net.DatagramSocket;
|
import java.net.InetAddress;
|
import java.net.Socket;
|
import java.net.SocketException;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
public class TextUdpActivity extends AppCompatActivity {
|
private static DatagramSocket datagramSocket7624 = null;
|
public static List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList = new ArrayList<>();
|
public static Timer searchTimer = null;
|
private static boolean isStop = false;
|
Button button;
|
public static void release() {
|
isStop = true;
|
datagramSocket7624.disconnect();
|
datagramSocket7624 = null;
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_text_udp);
|
button = findViewById(R.id.button1);
|
init7624();
|
button.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View view) {
|
SearchGateWay();
|
}
|
});
|
}
|
|
private static void SearchGateWay() {
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
byte[] sendBytes = new byte[44];
|
sendBytes[0] = (byte) 0xFE;
|
sendBytes[1] = 0x29;
|
sendBytes[2] = 0;
|
sendBytes[3] = 0;
|
sendBytes[4] = 0;
|
sendBytes[5] = 0;
|
sendBytes[6] = 0;
|
//获取所有网关(无需houseID模式),后面要改
|
sendBytes[7] = 0x67;
|
sendBytes[8] = 0x65;
|
sendBytes[9] = 0x74;
|
sendBytes[10] = 0x61;
|
sendBytes[11] = 0x6c;
|
sendBytes[12] = 0x6c;
|
sendBytes[13] = 0x67;
|
sendBytes[14] = 0x77;
|
for (int i = 15; i<sendBytes.length-1;i++){
|
sendBytes[i] = 0;
|
}
|
sendBytes[sendBytes.length - 1] = 0x02;
|
DatagramSocket socket = new DatagramSocket();
|
InetAddress address = InetAddress.getByName(NetWorkUtil.getLocalBroadCast());
|
DatagramPacket packet = new DatagramPacket(
|
sendBytes,
|
sendBytes.length,
|
address, Configuration.ZIGBEE_PORT);
|
socket.send(packet);
|
socket.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}).start();
|
|
}
|
|
/**
|
* 初始化7624端口
|
*/
|
public static void init7624(){
|
if (datagramSocket7624 != null){
|
HDLLog.info( "HDL SDK 7624 已启动");
|
}else {
|
HDLLog.info( "HDLSocket 7624 init");
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
datagramSocket7624 = new DatagramSocket(Configuration.ZIGBEE_PORT);
|
while (!isStop && datagramSocket7624 != null) {
|
datagramSocket7624.setReceiveBufferSize(1024 * 20);
|
datagramSocket7624.setReuseAddress(true);
|
while (datagramSocket7624 != null){
|
byte[] receiveBytes = new byte[1024];
|
DatagramPacket dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
|
if (datagramSocket7624 != null || (!datagramSocket7624.isClosed())) {
|
datagramSocket7624.receive(dataPacket);
|
}
|
Log.d("11114", "初始化端口接收的数据为receiveBytes: "+StringUtil.ByteArrToHex(receiveBytes,0,receiveBytes.length));
|
handleData(receiveBytes,dataPacket);
|
}
|
}
|
} catch (Exception e) {
|
datagramSocket7624 = null;
|
return;
|
}
|
}
|
}).start();
|
}
|
}
|
|
private static void handleData(byte[] receiveBytes,DatagramPacket dataPacket) {
|
if (receiveBytes[45] ==0){
|
return;
|
}
|
Log.d("11114", "处理的数据为receiveBytes: "+StringUtil.ByteArrToHex(receiveBytes,0,receiveBytes.length));
|
//是否主网关-01为主网关-00为从网关
|
String IsDomain = StringUtil.Byte2Hex(dataPacket.getData()[44]);
|
int headLength = 49;
|
if (receiveBytes.length > headLength) {
|
//网关名称长度
|
String str = StringUtil.Byte2Hex(dataPacket.getData()[49]);
|
int GwNameLength = Integer.parseInt(str, 16);
|
|
//网关名称
|
byte[] gwnamebyte = new byte[GwNameLength];
|
for (int i = 0; i < GwNameLength; i++) {
|
gwnamebyte[i] = dataPacket.getData()[i + 50];
|
}
|
String GwName = new String(gwnamebyte, 0, gwnamebyte.length);
|
|
//网关id长度
|
String str1 = StringUtil.Byte2Hex(dataPacket.getData()[50 + GwNameLength]);
|
int GwIdLength = Integer.parseInt(str1, 16);
|
|
//获取网关id
|
byte[] gatewayId = new byte[GwIdLength];
|
for (int i = 0; i < GwIdLength; i++) {
|
gatewayId[i] = dataPacket.getData()[i + 51 + GwNameLength];
|
}
|
|
if (gatewayId.length > 1) {
|
String stringGateWayId = new String(gatewayId, 0, gatewayId.length);
|
Log.d("11114", "网关id: " + stringGateWayId);
|
|
ZigbeeGatewayInfoBean zigbeeGatewayInfoBean = new ZigbeeGatewayInfoBean();
|
zigbeeGatewayInfoBean.setGatewayId(stringGateWayId);
|
zigbeeGatewayInfoBean.setIpAddress(dataPacket.getAddress().getHostAddress());
|
if (IsDomain.equals("01")){
|
zigbeeGatewayInfoBean.setDomain(true);
|
}else {
|
zigbeeGatewayInfoBean.setDomain(false);
|
}
|
zigbeeGatewayInfoBean.setGatewayName(GwName);
|
boolean isExit = false;
|
for (int i = 0; i < zigbeeGatewayInfoBeanList.size(); i++) {
|
if (zigbeeGatewayInfoBeanList.get(i).getGatewayId()
|
.equals(zigbeeGatewayInfoBean.getGatewayId())) {
|
isExit = true;
|
break;
|
}
|
}
|
if (!isExit) {
|
zigbeeGatewayInfoBeanList.add(zigbeeGatewayInfoBean);
|
Log.d("11114", "addList:成功添加 ");
|
}
|
}
|
}
|
}
|
}
|