'How do I get a device's gateway in Unity?

I'm trying to receive the standard gateway of the device in Unity and C# on an Android smartphone, but I just can't fix it.

That's the reason why I'm asking if there is a possibility to find a gateway on an Android smartphone using Unity and C#.

I already tried this one, which does work in the Unity editor:

    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in adapters)
    {
        IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
        GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
        if (addresses.Count > 0)
        {
            foreach (GatewayIPAddressInformation address in addresses)
            {
                if (address.Address.ToString() != "0.0.0.0")
                {
                    Debug.Log("Gateway: " + address.Address.ToString());
                }
            }
        }
    }


Solution 1:[1]

Works in PC:

IPAddress GatewayAddress = NetworkInterface
                .GetAllNetworkInterfaces()
                .Where(n => n.OperationalStatus == OperationalStatus.Up)
                .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                .SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
                .Select(g => g?.Address)
                .Where(a => a != null)
                .Where(a => a.AddressFamily == AddressFamily.InterNetwork)
                .FirstOrDefault();

Solution 2:[2]

Create a Network.java file on Assets/Plugins/Android directory.

Assets/Plugins/Android/Network.java

package com.mynet;
 
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class Network{
    //Match the IP of class C address
    public static final String regexCIp = "^192\\.168\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
    //Match a class A address
    public static final String regexAIp = "^10\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
    //Match B address
    public static final String regexBIp = "^172\\.(1[6-9]|2\\d|3[0-1])\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)\\.(\\d{1}|[1-9]\\d|1\\d{2}|2[0-4]\\d|25\\d)$";
 
 
    public  String getHostIp() {
        String hostIp;
        Pattern ip = Pattern.compile("(" + regexAIp + ")|" + "(" + regexBIp + ")|" + "(" + regexCIp + ")");
        Enumeration<NetworkInterface> networkInterfaces = null;
        try {
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        InetAddress address;
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                address = inetAddresses.nextElement();
                String hostAddress = address.getHostAddress();
                Matcher matcher = ip.matcher(hostAddress);
                if (matcher.matches()) {
                    hostIp = hostAddress;
                    return hostIp;
                }
 
            }
        }
        return null;
    }
}

Use this script in C# code like below:

using UnityEngine;
using UnityEngine.UI;

public class MyIPScript: MonoBehaviour
{

    public Text ipText;

    public void Start()
    {
        string ip = GetIP();
        ipText.text = ip;
    }

    private string GetIP() {
        AndroidJavaObject jo = new AndroidJavaObject("com.mynet.Network");
        AndroidJavaClass act = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        return jo.Call<string>("getHostIp");
    }
}

NOTE: This will only work on Android devices. You will not see any IP address if you test it on your PC. So, build your unity project by selecting Android from Build Settings and generate apk file then install it on your Android Phone or VR Headset. I tested it on Oculus Quest and it worked perfectly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ycromix
Solution 2 Shariful Islam Mubin