get current ip-address

Hello everyone,

how do I get the current ip-address of my device?
I tried it with System.Net.NetworkInformation.GetAllNetworkInterfaces() but it's currently broken.

Is there another way to determine current ip?

Posts

  • TeHaTeHa DEMember ✭✭✭

    I'm using:

    WifiManager wifiManager = (WifiManager)context.GetSystemService(Service.WifiService);
    int ip = wifiManager.ConnectionInfo.IpAddress;
    
  • SavageSavage ATMember

    Thank you, works fine on wifi.

    But I also need the assigned ip-address of an active vpn-connection (over the network provider).

  • SavageSavage ATMember

    Nobody knows a workaround?

  • CheesebaronCheesebaron DKInsider, University mod

    Nobody knows a workaround?

    Do you really want a workaround if there is an official way?

    Read the second to last post in this bug report: https://code.google.com/p/android/issues/detail?id=15082

  • SavageSavage ATMember

    Thank you for that advice, but it would be nicer if I could use the same code on different devices ;-)
    Nevertheless, here that way:

    Java.Util.IEnumeration networkInterfaces = NetworkInterface.NetworkInterfaces; while(networkInterfaces.HasMoreElements) { Java.Net.NetworkInterface netInterface = (Java.Net.NetworkInterface)networkInterfaces.NextElement(); Console.WriteLine(netInterface.ToString()); }

    [lo][1][/::1%1%1][/127.0.0.1]

    [dummy0][2]

    [sit0][3]

    [ip6tnl0][4]

    [wlan0][5][/fe80::8e77:12ff:fe5a:6052%wlan0%5][/192.168.100.135]

    [ppp0][6][/10.0.0.1]

  •            //Getting the IP Address of the device fro Android.
                IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
                string ipAddress = string.Empty;
                if (addresses != null && addresses[0] != null)
                {
                    ipAddress = addresses[0].ToString();
                }
                else
                {
                    ipAddress = null;
                }
    
  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @TeHa said:
    I'm using:

    WifiManager wifiManager = (WifiManager)context.GetSystemService(Service.WifiService);
    int ip = wifiManager.ConnectionInfo.IpAddress;

    To get it in string form:

    WifiManager manager = (WifiManager)GetSystemService(Service.WifiService);
    int ip = manager.ConnectionInfo.IpAddress;
    
    string ipaddress = Android.Text.Format.Formatter.FormatIpAddress(ip);`
    
  • isaaxisaax DOMember

    For me work this in PCL Xamarin:

        public static string GetIPAddress()
        {
            var AllNetworkInterfaces = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);
            var IPAddress = "";
            foreach (var interfaces in AllNetworkInterfaces)
            {
                if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("eth0")) continue;
    
                var AddressInterface = (interfaces as Java.Net.NetworkInterface).InterfaceAddresses;
                foreach (var AInterface in AddressInterface)
                {
                    if(AInterface.Broadcast != null)
                        IPAddress = AInterface.Address.HostAddress;
                }
            }
                return IPAddress;
        }
    
  • DanH2DanH2 USMember

    All the answers I've seen to this question have only gotten the internal IP address of my device while on my home network (198.162.#.#). So I took a slightly different approach, and ask the internet more directly. ipify has a nice and simple endpoint for getting your IP address, that can be executed in your shared code. For example...

    var client = new HttpClient();
    var response = await client.GetAsync("https-//api-ipify-org/?format=json");
    var resultString = await response.Content.ReadAsStringAsync();
    
    var result = JsonConvert.DeserializeObject<IpResult>(resultString);
    
    var yourIp = result.Ip;
    

    Where "IpResult" is a POCO with a single string property named "Ip" (that you need to create, in addition to this code.)

    PS. I can't post links here (yet), so you'll need to fix the url in the GetAsync call, but you get the idea.

  • dinobdinob USUniversity ✭✭✭

    How to do this in Xamarin.Forms .NETStandard?

  • PresitaPresita Member ✭✭

    Hello everyone,

    How to List all devices(IP) info on your WLAN /router Programmatically in Xamarin visual studio(for Android)?

Sign In or Register to comment.