Android Build - Can't Get IP Address

All I want to do is get the IPv4 address of my Android Unity build displayed on the android phone. The code listed here by “Programmer” works on iOS devices, windows, and mac laptops: c# - How to get IP address of device in Unity 2018+? - Stack Overflow - it is not working for me on the Android phones (running Pie 9 or Nougat 7). I’ve tried multiple wi-fi networks, I’ve tried different Unity builds (2019.2.2, 2019.2.6, 2019.3.4, 2020 alpha, tried setting internet access to required, nothing. What is it about Android that is causing a problem? Keep in mind that my android project does show an ip address when mobile data is turned on, but only shows 127.0.0.1 when I’m trying to do a simple local area wi-fi communication thing. Again, it works fine on the iOS phone on the same wi-fi network - I see the ip address phone receives data using NetMQ from a laptop on the same network. It’s got to be some setting in the phone right?

This looks promising: https://answers.unity.com/questions/1602555/https-requests-removed-on-android-pie-api-28.html

For what it’s worth, I tried adding the network_security_config.xml idea with the following in Plugins>Android>res>xml :

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

    <base-config
        cleartextTrafficPermitted="true">

        <trust-anchors>

            <certificates
                src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

And I added the much talked about android:usesCleartextTraffic = “true” in the tag of the AndroidManifest.xml. Both options failed in my case. See what I’m trying to do, like many others, is just have a TCP socket to a local network ipaddress and port to send and receive data for a game using NetMQ. Simple. Works fine with iOS.

This code returns the IP address of the device. But 127.0.0.1 is returned last in the address list. I added conditions that exclude this address and it worked for me.

public class IPManager
{
    public static string GetIP(ADDRESSFAM Addfam)
    {
        //Return null if ADDRESSFAM is Ipv6 but Os does not support it
        if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
        {
            return null;
        }

        string output = "0.0.0.0";

        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
            NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

            if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
#endif
            {
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {
                    //IPv4
                    if (Addfam == ADDRESSFAM.IPv4)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            if (ip.Address.ToString() != "127.0.0.1")
                                output = ip.Address.ToString();
                        }
                    }

                    //IPv6
                    else if (Addfam == ADDRESSFAM.IPv6)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            if (ip.Address.ToString() != "127.0.0.1")
                                output = ip.Address.ToString();
                        }
                    }
                }
            }
        }
        return output;
    }
}

public enum ADDRESSFAM
{
    IPv4, IPv6
}
2 Likes

How to decide whether to ask for IPv4 or IPv6?
I’m calling this function with:
_localIpLabel.text = IPManager.GetIP(ADDRESSFAM.IPv4);
But it seems that I have to choose between IPv4 or IPv6

Hello - this is what I have been using - it works on Windows and Android:

    // "Mr. Wang from Next Door" on StackOverflow: https://stackoverflow.com/questions/6803073/get-local-ip-address
    public static IPAddress GetLocalIP()
    {
        using(Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
        {
            try
            {
                socket.Connect("8.8.8.8", 65530);
                IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                return endPoint.Address;
            }
            catch (SocketException)
            {
                return IPAddress.Loopback;
    }   }   }