get WiFi IP address by converting int IP address to string - Android android.net.wifi

Android examples for android.net.wifi:Wifi Address

Description

get WiFi IP address by converting int IP address to string

Demo Code

import java.net.Inet4Address;

import android.content.Context;
import android.net.wifi.WifiManager;

public class Main {

  public static String getWiFiLYIP(Context context) {
    WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String ip = ipIntToString(mWifiManager.getDhcpInfo().gateway);
    return "http://" + ip;
  }/*from ww w.  j a v a  2 s .  c  om*/

  private static String ipIntToString(int ip) {
    try {
      byte[] bytes = new byte[4];
      bytes[0] = (byte) (0xff & ip);
      bytes[1] = (byte) ((0xff00 & ip) >> 8);
      bytes[2] = (byte) ((0xff0000 & ip) >> 16);
      bytes[3] = (byte) ((0xff000000 & ip) >> 24);
      return Inet4Address.getByAddress(bytes).getHostAddress();
    } catch (Exception e) {
      return "";
    }
  }

}

Related Tutorials