<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_alignParentTop="true"
+ android:layout_alignParentRight="true"
android:text="Stop"
/>
<Button android:id="@+id/btn_start"
android:layout_width="wrap_content"
- android:layout_height="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_alignParentTop="true"
+ android:layout_toLeftOf="@id/btn_stop"
android:text="Start"
/>
+<EditText android:id="@+id/text_entry"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_alignParentTop="true"
+ android:layout_toLeftOf="@id/btn_start"
+ android:digits="01234567890"
+ android:text="9999"
+ />
<TextView android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_below="@id/text_entry"
android:text="UdpRespondActivity"
/>
-</LinearLayout>
+</RelativeLayout>
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 {
+ private TextView tv;
+ private EditText et;
+
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Bundle bdl = msg.getData();
- updateUI(bdl.getString("ln"));
+ tv.append(bdl.getString("ln"));
}
};
- private TextView tv;
-
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
- tv.append("Start pressed\n");
+ 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\n");
+ tv.append("Stop pressed, exiting\n");
finish();
}
};
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);
startBgThread();
}
- private void updateUI(String str) {
- tv.append(str);
+ 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() {
- while (true) {
- SystemClock.sleep(3000);
- Bundle bdl = new Bundle();
- bdl.putString("ln", "this is msg\n");
- Message msg = Message.obtain(mHandler);
- msg.setData(bdl);
- msg.sendToTarget();
- }
+ 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());
+ }
+ }
}