diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-27 14:13:30 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-27 14:13:30 +0100 |
commit | 2570d2c6f0be6bb16630b5685063501dd931b4d2 (patch) | |
tree | 84fb900523b076c2e1205236ef712327e53c7df9 /same-android/src/main/java/com/orbekk | |
parent | da760fca66619e67d5a63e02768282fd6068d1f0 (diff) |
Refactor Android Broadcaster class.
– Add functionality to find current wi-fi IP.
– Clean up resulting code.
Diffstat (limited to 'same-android/src/main/java/com/orbekk')
-rw-r--r-- | same-android/src/main/java/com/orbekk/discovery/Broadcaster.java (renamed from same-android/src/main/java/com/orbekk/discovery/Broadcast.java) | 28 | ||||
-rw-r--r-- | same-android/src/main/java/com/orbekk/discovery/SameService.java | 6 |
2 files changed, 23 insertions, 11 deletions
diff --git a/same-android/src/main/java/com/orbekk/discovery/Broadcast.java b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java index c674f65..167eda1 100644 --- a/same-android/src/main/java/com/orbekk/discovery/Broadcast.java +++ b/same-android/src/main/java/com/orbekk/discovery/Broadcaster.java @@ -14,23 +14,19 @@ import android.content.Context; import android.net.DhcpInfo; import android.net.wifi.WifiManager; -public class Broadcast { +public class Broadcaster { private Context context; private Logger logger = LoggerFactory.getLogger(getClass()); private DatagramSocket socket = null; - public Broadcast(Context context) { + public Broadcaster(Context context) { this.context = context; } - public synchronized InetAddress getBroadcastAddress() { - WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); - DhcpInfo dhcp = wifi.getDhcpInfo(); - - int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; + public InetAddress fromInt(int ip) { byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) - quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); + quads[k] = (byte) ((ip >> k * 8) & 0xFF); try { return InetAddress.getByAddress(quads); } catch (UnknownHostException e) { @@ -39,6 +35,22 @@ public class Broadcast { } } + 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 sendBroadcast(byte[] data, int port) { try { socket = new DatagramSocket(0); diff --git a/same-android/src/main/java/com/orbekk/discovery/SameService.java b/same-android/src/main/java/com/orbekk/discovery/SameService.java index 7ca9778..68a6c0b 100644 --- a/same-android/src/main/java/com/orbekk/discovery/SameService.java +++ b/same-android/src/main/java/com/orbekk/discovery/SameService.java @@ -21,10 +21,10 @@ public class SameService extends Service { private Thread discoveryThread = null; public final class DiscoveryThread extends Thread { - Broadcast broadcast; + Broadcaster broadcast; public DiscoveryThread() { - broadcast = new Broadcast(SameService.this); + broadcast = new Broadcaster(SameService.this); } @Override public void run() { @@ -69,7 +69,7 @@ public class SameService extends Service { private void sendBroadcastDiscovery() { byte[] data = "Discover".getBytes(); - new Broadcast(this).sendBroadcast(data, PORT); + new Broadcaster(this).sendBroadcast(data, PORT); } private void joinNetwork() { |