android socket连接及发送数据


	@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);
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值