get Wifi InetAddress - Android android.net.wifi

Android examples for android.net.wifi:Wifi Address

Description

get Wifi InetAddress

Demo Code

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Main {

  public static InetAddress getWifiInetAddress() {
    try {//w  ww . j  a  va2s . c  o m
      for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        if (intf.getName().contains("wlan")) {
          for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
              return inetAddress;
            }
          }
        }
      }
    } catch (SocketException e) {
      e.printStackTrace();
    }

    return null;
  }

}

Related Tutorials