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);
}
这个主要就是大概的代码,如果有需要实现这个功能的可以私聊我完整代码的。