Get my wifi ip address Android

后端 未结 9 1474
無奈伤痛
無奈伤痛 2020-11-27 02:41

How can I get the ip address of my phone when it is connected under wifi?

I found a method here but it returns something like 24.182.239.255 even if I\'m under wifi

相关标签:
9条回答
  • 2020-11-27 03:38

    If you would like to get the private IP address of your device when connected to Wi-Fi, you can try this.

    WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    String ipAddress = Formatter.formatIpAddress(ip);
    

    Be sure to add the permission

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    

    to your manifest.

    0 讨论(0)
  • 2020-11-27 03:38

    This will get you the WiFi IPv4, IPv6 or both.

    public static Enumeration<InetAddress> getWifiInetAddresses(final Context context) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        final String macAddress = wifiInfo.getMacAddress();
        final String[] macParts = macAddress.split(":");
        final byte[] macBytes = new byte[macParts.length];
        for (int i = 0; i< macParts.length; i++) {
            macBytes[i] = (byte)Integer.parseInt(macParts[i], 16);
        }
        try {
            final Enumeration<NetworkInterface> e =  NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                final NetworkInterface networkInterface = e.nextElement();
                if (Arrays.equals(networkInterface.getHardwareAddress(), macBytes)) {
                    return networkInterface.getInetAddresses();
                }
            }
        } catch (SocketException e) {
            Log.wtf("WIFIIP", "Unable to NetworkInterface.getNetworkInterfaces()");
        }
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public static<T extends InetAddress> T getWifiInetAddress(final Context context, final Class<T> inetClass) {
        final Enumeration<InetAddress> e = getWifiInetAddresses(context);
        while (e.hasMoreElements()) {
            final InetAddress inetAddress = e.nextElement();
            if (inetAddress.getClass() == inetClass) {
                return (T)inetAddress;
            }
        }
        return null;
    }
    

    Usage:

    final Inet4Address inet4Address = getWifiInetAddress(context, Inet4Address.class);
    final Inet6Address inet6Address = getWifiInetAddress(context, Inet6Address.class);
    

    And don't forget:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    0 讨论(0)
  • 2020-11-27 03:46

    Based on my crash logs, it appears not every device returns the WiFi mac address.

    Here is a cleaner version of the most popular reply.

    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.putInt(wifiInfo.getIpAddress());
    try {
    final InetAddress inetAddress = InetAddress.getByAddress(null, byteBuffer.array());
    } catch (UnknownHostException e) {
        //TODO: Return null?
    }
    
    0 讨论(0)
提交回复
热议问题