diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-13 18:05:13 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-13 18:05:13 +0100 |
commit | 947390013409e297cab8ec991212473ae4b76802 (patch) | |
tree | 9cd271a33efd3a2b05154010711bdcd67bdd5e0a /same-android/src/main/java/com/orbekk/same | |
parent | 585f0a27869c230917c220962e0faf954a283e81 (diff) |
Android Broadcaster implements BroadcastInterface.
Diffstat (limited to 'same-android/src/main/java/com/orbekk/same')
-rw-r--r-- | same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java b/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java new file mode 100644 index 0000000..6e426e8 --- /dev/null +++ b/same-android/src/main/java/com/orbekk/same/android/net/Broadcaster.java @@ -0,0 +1,102 @@ +package com.orbekk.same.android.net; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.SocketException; +import java.net.UnknownHostException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.orbekk.net.BroadcasterInterface; + +import android.content.Context; +import android.net.DhcpInfo; +import android.net.wifi.WifiManager; + +public class Broadcaster implements BroadcasterInterface { + private Context context; + private Logger logger = LoggerFactory.getLogger(getClass()); + private DatagramSocket socket = null; + + public Broadcaster(Context context) { + this.context = context; + } + + public InetAddress fromInt(int ip) { + byte[] quads = new byte[4]; + for (int k = 0; k < 4; k++) + quads[k] = (byte) ((ip >> k * 8) & 0xFF); + try { + return InetAddress.getByAddress(quads); + } catch (UnknownHostException e) { + logger.warn("Failed to find broadcast address."); + return null; + } + } + + public DhcpInfo getDhcpInfo() { + WifiManager wifi = + (WifiManager)context.getSystemService(Context.WIFI_SERVICE); + return wifi.getDhcpInfo(); + } + + public synchronized InetAddress getWlanAddress() { + return fromInt(getDhcpInfo().ipAddress); + } + + public synchronized InetAddress getBroadcastAddress() { + DhcpInfo dhcp = getDhcpInfo(); + int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; + return fromInt(broadcast); + } + + public synchronized boolean sendUdpData(byte[] data, InetAddress ip, int port) { + try { + socket = new DatagramSocket(0); + socket.setBroadcast(true); + DatagramPacket packet = new DatagramPacket(data, data.length, ip, port); + socket.send(packet); + return true; + } catch (SocketException e) { + logger.warn("Failed to send broadcast.", e.fillInStackTrace()); + return false; + } 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(); + } + } + + @Override + public synchronized boolean sendBroadcast(int port, byte[] data) { + InetAddress broadcastAddress = getBroadcastAddress(); + return sendUdpData(data, broadcastAddress, port); + } +}
\ No newline at end of file |