Android UDP通信协议

来源:互联网 发布:fm2016汉化补丁 mac 编辑:程序博客网 时间:2024/05/29 14:26

1、定义一个接口类来定义协议中的端口与地址 

package com.sunplus.app.dao.impl;public interface IAcceptServerData {    public static final String SERVERIP = "255.255.255.255"; // 广播地址    public static final int SERVERPORT = 9600; // 端口号    public static final int temp = 1;    public static final int light = temp + 1;    public static final int humi = light + 1;    public void acceptUdpData(String data, int id);}
2、UDP协议的工具类

public class Tools {    public static String getServerData(String str) {        String acceptStr = null;        try {            InetAddress serverAddr = InetAddress.getByName(IAcceptServerData.SERVERIP);            DatagramSocket socket = new DatagramSocket();            byte[] buf = str.getBytes();            DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, IAcceptServerData.SERVERPORT);            socket.send(packet);            byte[] buffer = new byte[packet.getLength()];            DatagramPacket recvPacket = new DatagramPacket(buffer, buffer.length);            socket.receive(recvPacket);            InetAddress ad = recvPacket.getAddress();            String s = ad.getHostAddress();            acceptStr = new String(recvPacket.getData());        } catch (Exception e) {            e.printStackTrace();        }        return acceptStr;    }}
3、程序中调用 
package com.sunplus.app.activity;import org.achartengine.model.TimeSeries;import android.app.Activity;import android.app.AlertDialog;import android.app.DatePickerDialog;import android.app.Dialog;import android.app.TimePickerDialog;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.InputType;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.EditText;import android.widget.TextView;import android.widget.TimePicker;import com.sunplus.app.dao.impl.IAcceptServerData;import com.sunplus.app.util.JavaUtils;import com.sunplus.app.util.Tools;public class MainActivity extends Activity implements IAcceptServerData {    private Button lightBtn = null;    private Button tempBtn = null;    private Button humiBtn = null;    private boolean start;    private TextView nodeName = null;    private TextView tempValue = null;    private String temp = null; // 温度    private String light = null; // 光照度    private String humi = null; // 湿度    private Button dialogClose = null;    private Dialog dia = null;    private TextView ligthValue = null;    private TextView humiValue = null;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById();        start = false;        TimeSeries series = new TimeSeries(humi);    }    // 获取界面的组件,触发事件    private void findViewById() {        this.lightBtn = (Button) this.findViewById(R.id.light);        this.lightBtn.setText("光照度");        this.lightBtn.setOnClickListener(new BtnOnclickImpl());        this.tempBtn = (Button) this.findViewById(R.id.temp);        this.tempBtn.setText("温度");        this.tempBtn.setOnClickListener(new BtnOnclickImpl());        this.humiBtn = (Button) this.findViewById(R.id.humi);        this.humiBtn.setText("湿度");        this.humiBtn.setOnClickListener(new BtnOnclickImpl());    }    private class BtnOnclickImpl implements View.OnClickListener {        @Override        public void onClick(View v) {            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_node_values, null);            nodeName = (TextView) view.findViewById(R.id.node_name);            tempValue = (TextView) view.findViewById(R.id.temp_valuse);            ligthValue = (TextView) view.findViewById(R.id.light_valuse);            humiValue = (TextView) view.findViewById(R.id.humi_valuse);            switch (v.getId()) {            case R.id.temp:                new Thread(new AcceptThread("qt", IAcceptServerData.temp)).start();                tempValue.setVisibility(View.VISIBLE);                nodeName.setText("temp:");                break;            case R.id.light:                new Thread(new AcceptThread("ql", IAcceptServerData.light)).start();                ligthValue.setVisibility(View.VISIBLE);                nodeName.setText("light:");                break;            case R.id.humi:                new Thread(new AcceptThread("qh", IAcceptServerData.humi)).start();                humiValue.setVisibility(View.VISIBLE);                nodeName.setText("humi:");                break;            default:                break;            }         }    }    private Handler MyHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {            case IAcceptServerData.temp:                temp = (String) msg.obj;                tempValue.setText(temp);                android.util.Log.d("mark", "temp=" + temp);                break;            case IAcceptServerData.light:                light = (String) msg.obj;                ligthValue.setText(light);                android.util.Log.d("mark", "light=" + light);                break;            case IAcceptServerData.humi:                humi = (String) msg.obj;                humiValue.setText(humi);                android.util.Log.d("mark", "humi=" + humi);                break;            default:                break;            }        }    };    public class AcceptThread implements Runnable {        private String str;        private int id;        public AcceptThread(String str, int id) {            this.str = str;            this.id = id;        }        @Override        public void run() {            while (true) {                try {                    Thread.sleep(3000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                String temp = Tools.getServerData(str);                acceptUdpData(temp, id);            }        }    }    @Override    public void acceptUdpData(String data, int id) {        Message msg = new Message();        switch (id) {        case IAcceptServerData.temp:            msg.what = IAcceptServerData.temp;            break;        case IAcceptServerData.light:            msg.what = IAcceptServerData.light;            break;        case IAcceptServerData.humi:            msg.what = IAcceptServerData.humi;            break;        default:            break;        }        msg.obj = data;        MyHandler.sendMessage(msg);    }}


原创粉丝点击