diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-26 15:57:57 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-26 15:57:57 +0100 |
commit | c2c1e4c5aad74a7fbca5c281f9d8ea2d0ba99ad3 (patch) | |
tree | 03fe8c0a8e3eb1d9d0bac8b3545f08d0d916d8f7 | |
parent | 55beb6df753604996ae61f8a099db40f0906804e (diff) |
Add Discovery service.
DiscoveryService listens to UDP broadcasts.
-rw-r--r-- | same-android/AndroidManifest.xml | 1 | ||||
-rw-r--r-- | same-android/src/main/java/com/orbekk/discovery/Broadcast.java (renamed from same-android/src/main/java/com/orbekk/Broadcast.java) | 34 | ||||
-rw-r--r-- | same-android/src/main/java/com/orbekk/discovery/DiscoveryService.java | 76 |
3 files changed, 107 insertions, 4 deletions
diff --git a/same-android/AndroidManifest.xml b/same-android/AndroidManifest.xml index 6a596ed..66ceb4f 100644 --- a/same-android/AndroidManifest.xml +++ b/same-android/AndroidManifest.xml @@ -11,6 +11,7 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> + <service android:name="com.orbekk.discovery.DiscoveryService"/> </application> </manifest> diff --git a/same-android/src/main/java/com/orbekk/Broadcast.java b/same-android/src/main/java/com/orbekk/discovery/Broadcast.java index 84b2d6e..5f592db 100644 --- a/same-android/src/main/java/com/orbekk/Broadcast.java +++ b/same-android/src/main/java/com/orbekk/discovery/Broadcast.java @@ -1,4 +1,4 @@ -package com.orbekk; +package com.orbekk.discovery; import java.io.IOException; import java.net.DatagramPacket; @@ -17,12 +17,13 @@ import android.net.wifi.WifiManager; public class Broadcast { private Context context; private Logger logger = LoggerFactory.getLogger(getClass()); + private DatagramSocket socket = null; public Broadcast(Context context) { this.context = context; } - public InetAddress getBroadcastAddress() { + public synchronized InetAddress getBroadcastAddress() { WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); @@ -38,9 +39,9 @@ public class Broadcast { } } - public boolean sendBroadcast(byte[] data, int port) { + public synchronized boolean sendBroadcast(byte[] data, int port) { try { - DatagramSocket socket = new DatagramSocket(port); + socket = new DatagramSocket(port); socket.setBroadcast(true); DatagramPacket packet = new DatagramPacket(data, data.length, getBroadcastAddress(), port); socket.send(packet); @@ -51,6 +52,31 @@ public class Broadcast { } catch (IOException e) { logger.warn("Error when sending broadcast.", e.fillInStackTrace()); return false; + } finally { + socket.close(); + socket = null; + } + } + + public synchronized DatagramPacket receiveBroadcast(int port) { + try { + socket = new DatagramSocket(port); + byte[] data = new byte[1024]; + DatagramPacket packet = new DatagramPacket(data, data.length); + socket.receive(packet); + return packet; + } catch (IOException e) { + logger.warn("Failed to receive broadcast.", e); + return null; + } finally { + socket.close(); + socket = null; + } + } + + public void interrupt() { + if (socket != null) { + socket.close(); } } }
\ No newline at end of file diff --git a/same-android/src/main/java/com/orbekk/discovery/DiscoveryService.java b/same-android/src/main/java/com/orbekk/discovery/DiscoveryService.java new file mode 100644 index 0000000..e9aac5f --- /dev/null +++ b/same-android/src/main/java/com/orbekk/discovery/DiscoveryService.java @@ -0,0 +1,76 @@ +package com.orbekk.discovery; + +import java.net.DatagramPacket; + +import android.app.Service; +import android.content.Intent; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.widget.Toast; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DiscoveryService extends Service { + private Logger logger = LoggerFactory.getLogger(getClass()); + private Thread thread = null; + + public final class DiscoveryThread extends Thread { + Broadcast broadcast; + + public DiscoveryThread() { + broadcast = new Broadcast(DiscoveryService.this); + } + + @Override public void run() { + while (!Thread.interrupted()) { + byte[] data = new byte[1024]; + DatagramPacket packet = broadcast.receiveBroadcast(15066); + String content = new String(packet.getData(), 0, packet.getLength()); + Message message = Message.obtain(); + message.obj = content; + toastHandler.sendMessage(message); + } + } + + @Override public void interrupt() { + super.interrupt(); + broadcast.interrupt(); + } + } + + private Handler toastHandler = new Handler() { + @Override public void handleMessage(Message message) { + Toast.makeText(DiscoveryService.this, + (String)message.obj, Toast.LENGTH_SHORT) + .show(); + logger.info("Display toast: {}", (String)message.obj); + } + }; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Toast.makeText(this, "service start: " + intent.getAction(), + Toast.LENGTH_SHORT).show(); + if (thread == null) { + synchronized (this) { + thread = new DiscoveryThread(); + thread.start(); + } + } + return START_STICKY; + } + + @Override + public void onDestroy() { + Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show(); + thread.interrupt(); + } + +} |