Getting IP address in a readable form android code

后端 未结 4 931
囚心锁ツ
囚心锁ツ 2021-01-26 04:31

I am new to android development and I am doing an application which sends the IP address of an android device to another one by sms. I need to get the IP in decimal like this 19

4条回答
  •  一整个雨季
    2021-01-26 04:58

    While hungr's answer is correct, I found that if I loop through the ip_addresses for a specific device "wlan0" for example, the first address is ipv6 and the second is ipv4. I was only returning the first value, which is why I was only getting a hex string.

    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                    String ip_address = inetAddress.getHostAddress();
                    Log.d(APP_NAME, "IP: " + ip_address);
                    //return if_name + ": " + ip_address;
    }
    

    Notice I commented out the "return"

提交回复
热议问题