Android IP Address Get getIPAddress(boolean useIPv4)

Here you can find the source of getIPAddress(boolean useIPv4)

Description

Get IP address from first non-localhost interface

License

Apache License

Parameter

Parameter Description
ipv4 true=return ipv4, false=return ipv6

Return

address or empty string

Declaration

public static String getIPAddress(boolean useIPv4) 

Method Source Code

//package com.java2s;
/**/*from  ww w.  jav a  2 s.c  om*/
 * Copyright (c) 2013-2014, Rinc Liu (http://rincliu.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
import org.apache.http.conn.util.InetAddressUtils;

public class Main {
    /**
     * Get IP address from first non-localhost interface
     * 
     * @param ipv4 true=return ipv4, false=return ipv6
     * @return address or empty string
     */
    public static String getIPAddress(boolean useIPv4) {
        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()) {
                        String sAddr = addr.getHostAddress().toUpperCase();
                        boolean isIPv4 = InetAddressUtils
                                .isIPv4Address(sAddr);
                        if (useIPv4) {
                            if (isIPv4)
                                return sAddr;
                        } else {
                            if (!isIPv4) {
                                int delim = sAddr.indexOf('%'); // drop ip6
                                                                // port suffix
                                return delim < 0 ? sAddr : sAddr.substring(
                                        0, delim);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related

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