Android IP Address Get getLocalIpAddress()

Here you can find the source of getLocalIpAddress()

Description

get Local Ip Address

Declaration

public static InetAddress getLocalIpAddress() 

Method Source Code

//package com.java2s;

import android.util.Log;

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

import java.util.Enumeration;

public class Main {
    public static InetAddress getLocalIpAddress() {
        try {/*from  w  w  w .java  2s  .c o m*/
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        Log.i("IP", inetAddress.getHostAddress());
                        return inetAddress;
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("IPNo", ex.toString());
        }
        return null;
    }
}

Related

  1. getIPAddr()
  2. getByteAddress(BluetoothDevice device)
  3. getInetIpAddress()
  4. getIPAddress(boolean useIPv4)
  5. getHostAddress(int address)
  6. getLocalHostAddress()
  7. getIPAddress(boolean useIPv4)