Get my wifi ip address Android

后端 未结 9 1519
無奈伤痛
無奈伤痛 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: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?
    }
    

提交回复
热议问题