@Override
public void run() {
try {
Socket socket = new Socket(Constants.HOST, Constants.PORT);
sendLocalBroadcast(Constants.DEVICE_CONNECTED);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
initTime = System.currentTimeMillis();
frameNum = 0;
while (true) {
byte[] head = new byte[4];
inputStream.read(head, 0, 4);
if (!Arrays.equals(head, SendCommand.HEADER)) continue;
byte[] cmd = new byte[4];
inputStream.read(cmd, 0, 4);
switchCmd(cmd);
}
} catch (IOException e) {
sendLocalBroadcast(Constants.DEVICE_DISCONNECTED);
}
}
public void sendData(byte[] msg) {
if (outputStream != null) {
try {
outputStream.write(msg);
sendLocalBroadcast(Constants.SEND_MSG_SUCCEED);
} catch (IOException e) {
sendLocalBroadcast(Constants.SEND_MSG_ERROR);
}
} else {
sendLocalBroadcast(Constants.DEVICE_DISCONNECTED);
}
}
public class SendCommand {
public static final byte[] HEADER = {(byte) 0x44, (byte) 0x4D, (byte) 0xAA, (byte) 0xBB};
//1:解、冻结
public static byte[] c_freeze() {//冻结
byte[] cmd = {0x00, 0x00, 0x00, 0x30};
byte[] type = {0x00, 0x00, 0x00, 0x01};
byte[] len = {0x00, 0x00, 0x00, 0x01};
byte[] param = {0x00};
return compose(cmd, type, len, param);
}
public static byte[] c_get_CENTER() {
byte[] cmd = {0x00, 0x00, 0x00, 0X29};
byte[] type = {0x00, 0x00, 0x00, 0X10};
byte[] len = {0x00, 0x00, 0x00, 0x01};
byte[] param = {0x00};
return compose(cmd, type, len, param);
}
public static byte[] c_get_ROI() {
byte[] cmd = {0x00, 0x00, 0x00, 0X17};
byte[] type = {0x00, 0x00, 0x00, 0X03};
byte[] len = {0x00, 0x00, 0x00, 0x20};//20(为16进制,10进制为:32)
byte[] param = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
return compose(cmd, type, len, param);
}
private static byte[] compose(byte[] cmd, byte[] type, byte[] len, byte[] param) {
byte[] lastCommand = new byte[HEADER.length + cmd.length + type.length + len.length + param.length];
System.arraycopy(HEADER, 0, lastCommand, 0, HEADER.length);
System.arraycopy(cmd, 0, lastCommand, HEADER.length, cmd.length);
System.arraycopy(type, 0, lastCommand, HEADER.length + cmd.length, type.length);
System.arraycopy(len, 0, lastCommand, HEADER.length + cmd.length + type.length, len.length);
System.arraycopy(param, 0, lastCommand, HEADER.length + cmd.length + len.length + type.length, param.length);
return lastCommand;
}
}
private void onGetProbe() throws IOException {
byte[] type = new byte[4];
inputStream.read(type, 0, 4);
byte[] lenBytes = new byte[4];
inputStream.read(lenBytes, 0, 4);
int len = FormatUtils.bytesToInt(lenBytes);
byte[] param = new byte[len];
inputStream.read(param, 0, len);
int toInt = FormatUtils.bytesToInt(param);
Intent intent = new Intent(Constants.ACTION);
intent.putExtra("status", RECEIVED_GET_PROBE);
intent.putExtra("type", toInt == 1 ? false : true);
localBroadcastManager.sendBroadcast(intent);
}
android socket连接及发送数据
最新推荐文章于 2024-07-11 16:13:21 发布