get Broadcast Ip - Android android.net

Android examples for android.net:DhcpInfo

Description

get Broadcast Ip

Demo Code

import android.net.DhcpInfo;
import android.net.wifi.WifiManager;

public class Main {

  private static WifiManager mWifiManager;

  public static String getBroadcastIp() {
    DhcpInfo dinfo = mWifiManager.getDhcpInfo();
    int ip = changeEndian(dinfo.gateway);
    return StrOfLongIP(ip + 254);
  }/*from ww w .  ja  va  2  s . c om*/

  private static String StrOfLongIP(int longIP) {
    return String.valueOf(longIP >>> 24) + "." + String.valueOf((longIP & 0x00FFFFFF) >>> 16) + "."
        + String.valueOf((longIP & 0x0000FFFF) >>> 8) + "." + String.valueOf(longIP & 0x000000FF);
  }

  public static int changeEndian(int s) {
    int dest = 0;
    dest = ((s & 0x00ff00ff) << 8) + ((s & 0xff00ff00) >>> 8);
    dest = ((dest & 0x0000ffff) << 16) + ((dest & 0xffff0000) >>> 16);
    return dest;
  }

}

Related Tutorials