Android Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

The list of methods to do IP Address Get are organized into topic(s).

Method

StringgetIPAddr()
get IP Addr
InetAddress Address = null;
try {
    Address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
    e.printStackTrace();
return Address.getHostAddress();
byte[]getByteAddress(BluetoothDevice device)
get Byte Address
return getBytesFromAddress(device.getAddress());
StringgetInetIpAddress()
get Inet Ip Address
try {
    for (Enumeration<NetworkInterface> en = NetworkInterface
            .getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> ipAddr = intf
                .getInetAddresses(); ipAddr.hasMoreElements();) {
            InetAddress inetAddress = ipAddr.nextElement();
            return inetAddress.getHostAddress();
...
InetAddressgetLocalIpAddress()
get Local Ip Address
try {
    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()) {
...
StringgetIPAddress(boolean useIPv4)
Get IP address from first non-localhost interface
try {
    List<NetworkInterface> interfaces = Collections
            .list(NetworkInterface.getNetworkInterfaces());
    for (NetworkInterface intf : interfaces) {
        List<InetAddress> addrs = Collections.list(intf
                .getInetAddresses());
        for (InetAddress addr : addrs) {
            if (!addr.isLoopbackAddress()) {
...
StringgetHostAddress(int address)
Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
return ((address >>> 24) & 0xFF) + "." + ((address >>> 16) & 0xFF)
        + "." + ((address >>> 8) & 0xFF) + "."
        + ((address >>> 0) & 0xFF);
StringgetLocalHostAddress()
get Local Host Address
boolean useIPv4 = true;
try {
    List<NetworkInterface> interfaces = Collections
            .list(NetworkInterface.getNetworkInterfaces());
    for (NetworkInterface intf : interfaces) {
        List<InetAddress> addrs = Collections.list(intf
                .getInetAddresses());
        for (InetAddress addr : addrs) {
...
StringgetIPAddress(boolean useIPv4)
Get IP address from first non-localhost interface
try {
    List<NetworkInterface> networkInterfaces = Collections
            .list(NetworkInterface.getNetworkInterfaces());
    for (NetworkInterface networkInterface : networkInterfaces) {
        List<InetAddress> inetAddresses = Collections
                .list(networkInterface.getInetAddresses());
        for (InetAddress inetAddress : inetAddresses) {
            if (!inetAddress.isLoopbackAddress()) {
...
InetAddressgetTargetInetaddress(String target)
get Target Inetaddress
InetAddress targetInetAddress = null;
try {
    targetInetAddress = InetAddress.getByName(target.trim());
} catch (UnknownHostException e) {
    e.printStackTrace();
return targetInetAddress;
StringgetDevicesMac(Context context)
get Devices Mac
WifiManager wifiManager = (WifiManager) context
        .getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
    wifiManager.setWifiEnabled(true);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String mac = wifiInfo.getMacAddress().toString();
return mac;
...