]> www.average.org Git - UdpRespond.git/commitdiff
use datagram sockets master
authorEugene Crosser <crosser@average.org>
Sun, 8 Aug 2010 21:16:12 +0000 (01:16 +0400)
committerEugene Crosser <crosser@average.org>
Sun, 8 Aug 2010 21:16:12 +0000 (01:16 +0400)
AndroidManifest.xml
res/layout/main.xml
src/com/example/udpresponder/UdpRespondActivity.java

index 3abecdae2cfd051d542467666d643988705f93f7..09d64d1441fe0692d67f992ad4692307f22c0705 100644 (file)
@@ -3,6 +3,7 @@
       package="com.example.udpresponder"
       android:versionCode="1"
       android:versionName="1.0">
+    <uses-permission android:name="android.permission.INTERNET" />
     <application android:label="@string/app_name" android:icon="@drawable/icon">
         <activity android:name="UdpRespondActivity"
                   android:label="@string/app_name">
@@ -11,5 +12,7 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+       <service android:name="UdpRespondService">
+       </service>
     </application>
 </manifest> 
index 9caf5f68a4b11f3b1f3797332ca487df4ab27a67..1abab34b89b83734156425ea324b215edc96d58e 100644 (file)
@@ -1,23 +1,35 @@
 <?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>
 
index 7ef9709749f9909b2b3d7be5bf2ae562f7c5b0a1..30da0b4c66fb0bb479fe59d6f8e9ff9ce26c2b90 100644 (file)
@@ -1,35 +1,44 @@
 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();
                }
        };
@@ -40,6 +49,7 @@ public class UdpRespondActivity extends Activity {
                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);
@@ -47,23 +57,56 @@ public class UdpRespondActivity extends Activity {
                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());
+               }
+       }
 }