安卓用udp协议实现一个接发数据,并且能够以一定的反馈灯反馈回来。

1.首先接发数据的时候,要单独开一个线程。发送数据每一次按一下,开一个线程,发完之后线程关闭,这样的话可以不干扰其他操作。然后接收数据的时候,也是开一个线程,不过这个接收数据的线程里面写了一个while(true),也就是调用一次之后,会一直接收数据。
2.为了分别显示三组不同状态,使用三组radiogroup,然后每一组里面通过radiobutton的选中来作为指示灯。
具体总体布局效果图如下在这里插入图片描述
然后指示灯方面的xml布局如下

                <RadioButton
                    android:id="@+id/rb_h"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="  高"
                    android:checked="false"
                    android:enabled="false"
                    android:button="@drawable/rc_bg"
                    android:textColor="#222"
                    ></RadioButton>
                <View
                    android:layout_width="5dp"
                    android:layout_height="20dp"
                    />
                <RadioButton
                    android:id="@+id/rb_m"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="false"
                    android:enabled="false"
                    android:button="@drawable/rc_bg"
                    android:text="  中"
                    android:textColor="#222"
                    ></RadioButton>
                <View
                    android:layout_width="5dp"
                    android:layout_height="20dp"
                    />
                <RadioButton
                    android:id="@+id/rb_l"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="false"
                    android:enabled="false"
                    android:button="@drawable/rc_bg"
                    android:text="  低"
                    android:textColor="#222"
                    ></RadioButton>
            </RadioGroup>

``
因为采用的是udp协议收发数据,这里先写发送的方法

 public class UDPClient extends Service implements Runnable {
	 private  DatagramSocket socket;
	 private static DatagramPacket inpacket, outpacket;
	 private byte[] inBuff = new byte[6];//因为我这里等会接收的数据是6个字节的数组
	 private Handler handler;//定义这个是为了把接收到的信息抛给主线程,因为不能在子线程更新UI
     private UDPClient(){
        if(socket == null){
            try {
                socket = new DatagramSocket(3000);
                Log.d(TAG, "null");
            }catch (SocketException e){
                e.printStackTrace();
            }
        }else {
            Log.d(TAG, "not null");
        }
    }
     public static UDPClient getUdpClient(){//获取udp等会的单例对象
        return instance;
    }
    //udp发送数据的时候socket可以不指定端口号和ip,可以在它的集装箱outpacket指定端口号和ip地址
      public void send_data(final byte[] output_data){
      new Thread(new Runnable() {
            @Override
            public void run() {
                InetAddress address = null;
                try {
                    String IP = “192.168.1.101”
                    address = InetAddress.getByName(IP);
                } catch (UnknownHostException e)
                {
                    e.printStackTrace();
                }catch (Exception e){
                    e.printStackTrace();
                }
                //发送的数据报端口号,ip都指定好了
                outpacket = new DatagramPacket(output_data, output_data.length, address, 3000);
                try {
                    socket.send(outpacket);

                } catch (IOException e) {
                    e.printStackTrace();
                }}).start();
      }
//这个重写的方法是实现runnable接口的
      @Override
    public void run() {
    //定义一个接收数据的集装箱,然后指定装的东西类型是一个字节数组,长度
    inpacket = new DatagramPacket(inBuff, inBuff.length);
    while (true) {
        try {
            Log.d(TAG, "udp正在监听" + Thread.currentThread().getName());
            //如果socket没有接收到数据的话,就不会执行跳过下面,会阻塞在下面
            socket.receive(inpacket);
            String data=getBufHexStr(inBuff);
            String RcvMsg = new String(inpacket.getData(), inpacket.getOffset(), inpacket.getLength());
//           将收到的消息传到主线程采用handler
           Message msg = handler.obtainMessage();
           //判断接收的字节数组第2个索引是什么16进制字节
           switch(inBuff[1]){
               case 0x01:
                   Log.d(TAG, 01+"");
                   //这里加入what是为了确定是哪一个radiogroup,obj是哪一个radiobutton
                   msg.what=1;
                   msg.obj=data;
                   break;
               case 0x02:
                   Log.d(TAG, 02+"");
                   msg.what=2;
                   msg.obj=data;
                   break;
               case 0x03:
                   Log.d(TAG, 03+"");
                   msg.what=3;
                   msg.obj=data;
                   break;
           }
           handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//这个方法是将16字节数组转换成字符串
public static String getBufHexStr(byte [] raw){
        String s="0123456789ABCDEF";
        if(raw==null){
            return null;
        }
        final  StringBuilder hex=new StringBuilder(2*raw.length);
        for(final byte b:raw){
            hex.append(s.charAt((b&0xF0)>>4)).append(s.charAt((b&0x0F)));
        }
        return  hex.toString();
}
 }

然后主线程的方法如下

public class MainActivity extends AppCompatActivity implements 
View.OnTouchListener,View.OnClickListener  {
   private final MyHandler myHandler = new MyHandler(this);
   private  UDPClient udpClient;
   udpClient = UDPClient.getUdpClient();
      //这个是绑定udp的handler就是为main里面的myhandler
      udpClient.setHanler(myHandler);
   private static final byte[] BACK_UP = {0x0c,0x01,0x01,0x01,0x0E};
    private class MyHandler extends Handler{
        private final WeakReference<MainActivity> mActivity;
        public MyHandler(MainActivity activity){
            mActivity = new WeakReference<MainActivity>(activity);
        }
        //对handleMessage方法重写,接收数据
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                //这个处理是为了当接收到其他radiogroup组的信号的时候,比如风温组跳转到水压组,这个时候就应该把风稳组的所有信号灯全部清除掉
                    ((RadioGroup)findViewById(R.id.rg)).clearCheck();
                    ((RadioGroup)findViewById(R.id.rg3)).clearCheck();
                    Log.d(TAG, msg.obj+"");
                    switch ((String)msg.obj){
                        case "320101010134":
                            Log.d(TAG, "水温高 ");
                            ((RadioGroup)findViewById(R.id.rg2)).check(R.id.rb_h2);
                            break;
                        case "320101010234":
                            Log.d(TAG, "水温中");
                            ((RadioGroup)findViewById(R.id.rg2)).check(R.id.rb_m2);
                            break;
                        case "320101020134":
                            Log.d(TAG, "水温低");
                            ((RadioGroup)findViewById(R.id.rg2)).check(R.id.rb_l2);
                            break;
                    }
//                    Log.d(TAG, "handleMessage");
//                    byte[] res = (byte[])msg.obj;
//                    ((RadioGroup)findViewById(R.id.rg)).check(R.id.rb_h);
                    break;
                case 2:
                    ((RadioGroup)findViewById(R.id.rg)).clearCheck();
                    ((RadioGroup)findViewById(R.id.rg3)).clearCheck();
                    Log.d(TAG, msg.obj+"");
                    switch ((String)msg.obj){
                        case "320201010134":
                            Log.d(TAG, "水压低 ");
                            ((RadioGroup)findViewById(R.id.rg3)).check(R.id.rb_h3);
                            break;
                        case "320201010234":
                            Log.d(TAG, "水压中");
                            ((RadioGroup)findViewById(R.id.rg3)).check(R.id.rb_m3);
                            break;
                        case "320201020134":
                            Log.d(TAG, "水压高");
                            ((RadioGroup)findViewById(R.id.rg3)).check(R.id.rb_l3);
                            break;
                    }
                case 3:
                    ((RadioGroup)findViewById(R.id.rg)).clearCheck();
                    ((RadioGroup)findViewById(R.id.rg2)).clearCheck();
                    switch ((String)msg.obj){
                        case "320301010134":
                            Log.d(TAG, "风温低 ");
                            ((RadioGroup)findViewById(R.id.rg)).check(R.id.rb_h);
                            break;
                        case "320301010234":
                            Log.d(TAG, "风温中");
                            ((RadioGroup)findViewById(R.id.rg)).check(R.id.rb_m);
                            break;
                        case "320301020134":
                            Log.d(TAG, "风温高");
                            ((RadioGroup)findViewById(R.id.rg)).check(R.id.rb_l);
                            break;
                    }
            }
        }
    }
  new Thread(udpClient).start();
   udpClient.send_data(BACK_UP);
}

这个主要就是大概的代码,如果有需要实现这个功能的可以私聊我完整代码的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值