X-Git-Url: http://www.average.org/gitweb/?p=UdpRespond.git;a=blobdiff_plain;f=src%2Fcom%2Fexample%2Fudpresponder%2FUdpRespondActivity.java;h=30da0b4c66fb0bb479fe59d6f8e9ff9ce26c2b90;hp=b0e54ea607d7736f3546bf3de8a321bd57dc04df;hb=refs%2Fheads%2Fmaster;hpb=d53a621af624beaee08ea21438899d303235435b diff --git a/src/com/example/udpresponder/UdpRespondActivity.java b/src/com/example/udpresponder/UdpRespondActivity.java index b0e54ea..30da0b4 100644 --- a/src/com/example/udpresponder/UdpRespondActivity.java +++ b/src/com/example/udpresponder/UdpRespondActivity.java @@ -1,15 +1,112 @@ package com.example.udpresponder; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.DatagramPacket; +import java.net.DatagramSocket; import android.app.Activity; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.SystemClock; +import android.widget.TextView; +import android.widget.EditText; +import android.widget.Button; +import android.view.View; +import android.view.View.OnClickListener; -public class UdpRespondActivity extends Activity -{ - /** Called when the activity is first created. */ - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - } +public class UdpRespondActivity extends Activity { + + private TextView tv; + private EditText et; + + private Handler mHandler = new Handler() { + public void handleMessage(Message msg) { + Bundle bdl = msg.getData(); + tv.append(bdl.getString("ln")); + } + }; + + private OnClickListener mStartListener = new OnClickListener() { + public void onClick(View v) { + CharSequence cs = et.getText(); + tv.append("Start pressed, data: " + + cs.toString() + + " (unimplemented)\n"); + } + }; + + private OnClickListener mStopListener = new OnClickListener() { + public void onClick(View v) { + tv.append("Stop pressed, exiting\n"); + finish(); + } + }; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + tv = (TextView)findViewById(R.id.tv); + tv.append(" Starting\n"); + et = (EditText)findViewById(R.id.text_entry); + Button btn_start = (Button)findViewById(R.id.btn_start); + btn_start.setOnClickListener(mStartListener); + Button btn_stop = (Button)findViewById(R.id.btn_stop); + btn_stop.setOnClickListener(mStopListener); + startBgThread(); + } + + private void println(String str) { + Bundle bdl = new Bundle(); + bdl.putString("ln", str + "\n"); + Message msg = Message.obtain(mHandler); + msg.setData(bdl); + msg.sendToTarget(); + } + + protected void startBgThread () { + Thread t = new Thread() { + public void run() { + bgTask(); + } + }; + t.start(); + } + + private void bgTask() { + DatagramSocket s = null; + try { + s = new DatagramSocket(9999); + } catch (java.net.SocketException e) { + println("socket: " + e); + return; + } +// InetSocketAddress sa = new InetSocketAddress(9999); +// try { +// s.bind(sa); +// } catch (IllegalArgumentException e) { +// println("bind: " + e); +// return; +// } catch (java.net.SocketException e) { +// println("bind: " + e); +// return; +// } + println("Bound to port " + s.getLocalPort()); + while (true) { + byte[] data = new byte[256]; + DatagramPacket pkt = new DatagramPacket(data, 256); + try { + s.receive(pkt); + } catch (java.io.IOException e) { + println("receive: " + e); + } + InetAddress ia = pkt.getAddress(); + int ip = pkt.getPort(); + byte[] id = pkt.getData(); + println(ia.toString() + ":" + + ip + " " + + id.toString()); + } + } }