0

I have tried below code but it keeps on giving the me iPhone(ie.Device) IP Address. But I want to get a ROUTER IP ADDRESS

public string GetIPAddress()
    {
        String ipAddress = "";

        foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
        {
            Console.WriteLine(netInterface);
            if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
            {
                foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses)
                {
                    if (addrInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ipAddress = addrInfo.Address.ToString();
                    }
                }
            }
        }

iPhone IPAddress: 192.168.0.19 Router: 192.168.0.1

Wifi address config screen

I can do the Hacky way of removing the '9' from the last set of IP address. But I don't want to do this.

iTag
  • 409
  • 3
  • 19
  • I assume that is just Apple's terminology for "Gateway"? – ProgrammingLlama Feb 06 '23 at 05:58
  • 1
    [Does this answer your question?](https://stackoverflow.com/questions/13634868/get-the-default-gateway) – ProgrammingLlama Feb 06 '23 at 06:00
  • @ProgrammingLlama from iOS Side it works well. thanks for that. but from Android side. it throws an error: ```type=1400 audit(0.0:400): avc: denied { getattr } for path="/proc/8503/net/route" dev="proc" ino=4026532048 scontext=u:r:untrusted_app:s0:c151,c256,c512,c768 tcontext=u:object_r:proc_net:s0 tclass=file permissive=0 app=com.drawrect.tryrebex``` – iTag Feb 06 '23 at 08:58

1 Answers1

1

You can use the following code to get a ROUTER IP ADDRESS(Gateway address) from Android side.

Create a DependenceService in Forms folder.

public interface INetServices  
{  
    string ConvertGateway();  
} 

Then achieve this interface in the android folder.

[assembly: Dependency(typeof(NetService))]  
   namespace Forms.Droid  
   {  
       class NetService: INetServices  
       {  
           [Obsolete]
           public string ConvertGateway()  
           {  
               WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Service.WifiService);  
               int ip = wifiManager.ConnectionInfo.IpAddress;  
               int gateway = wifiManager.DhcpInfo.Gateway;

               IPAddress ipAddr = new IPAddress(ip);  
               IPAddress gatewayAddr = new IPAddress(gateway);
              
              
               return gatewayAddr.ToString();  
           }  
       }  
   }  

And add following permission in the AndroidManifest.xml

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

In the Xamarin forms, you can get ROUTER IP ADDRESS like following code.

var gatewayAddress = DependencyService.Get<INetServices>().ConvertGateway();            
Console.WriteLine(gatewayAddress);
Jianwei Sun - MSFT
  • 2,289
  • 1
  • 3
  • 7
  • It is giving `10.0.2.2` not real one ie(192.168.0.1) – iTag Feb 07 '23 at 08:21
  • @iTag When it's connected to WiFi, I tested the code in my Android phone, and it returns the real one (Gateway: 172.20.10.1) , IP Address: 172.20.10.1 What does `10.0.2.2` represent in your mobile phone? Or meaningless? – Jianwei Sun - MSFT Feb 07 '23 at 08:36
  • I have tested with Android Emulator and it connected to Wifi router. not on Real device. – iTag Feb 07 '23 at 09:11
  • i am getting the same 10.0.2.2 and it throws and some warning as well. i will check real device as well. `type=1400 audit(0.0:18): avc: denied { getattr } for path="/proc/3832/net/route" dev="proc" ino=4026532048 scontext=u:r:untrusted_app:s0:c151,c256,c512,c768 tcontext=u:object_r:proc_net:s0 tclass=file permissive=0 app=com.drawrect.tryrebex ` – iTag Feb 07 '23 at 09:15
  • @iTag I also think you should test it on the real machine. The same error reported twice is probably the reason for the Android Emulator. – Jianwei Sun - MSFT Feb 07 '23 at 09:37
  • Your code works well. i have tested with real device. i got the router gateway ip address. – iTag Feb 07 '23 at 12:12