Android UDP通信协议

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);

    }


}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值