question

SimonQuin-5771 avatar image
0 Votes"
SimonQuin-5771 asked LouisMulder-3553 commented

Obtain IP address from host name on Android using Xamarin

I wrote an app using WinForms in C#, part of which was to access a device on my local network using its name rather than IP address. This used the function Dns.GetHostEntry() which works as expected. I then created an Android application using Xamarin forms and found that this function is not supported. What is the recommended method of obtaining the IP address from the host name on this platform?

dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LouisMulder-3553 commented

Hello,​

Welcome to our Microsoft Q&A platform!

For get IP address in Android, you can create a dependenceService in PCL.

public interface INetServices
    {
        string ConvertHostIP();
    }


Then achieve this interface in the android folder.

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Xamarin.Forms;

[assembly: Dependency(typeof(NetService))]
namespace MultilingualXFSample.Droid
{
    class NetService: INetServices
    {

        public string ConvertHostIP()
        {
            WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Service.WifiService);
            int ip = wifiManager.ConnectionInfo.IpAddress;

            IPAddress ipAddr = new IPAddress(ip);

         
          //  System.out.println(host);
            return ipAddr.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 IP address like following code.

string IPAddress= DependencyService.Get<INetServices>().ConvertHostIP();


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Leon, thanks ever so much for your kind answer. Although I have not yet tried it I am guessing this returns the IP address allocated to the device this code is running on?
Actually what I am after is being able to get the IP address of another device on the same local subnet by using its name. So I was looking for a function like:

ReqdIpAddress = GetIpFromName("TheSentinel");

Where "TheSentinel" is a network device with that name.

I am sure what you have sent me already will point me in the right direction.

Regards....Simon

0 Votes 0 ·

I am guessing this returns the IP address allocated to the device this code is running on?

Yes.

0 Votes 0 ·

Is that worked about above code?

0 Votes 0 ·

I have not yet solved this problem but I will post any solution I find here. I hope to get back to it shortly.

0 Votes 0 ·

Ok, that is fine.

0 Votes 0 ·

I am afraid I may need some assistance to get your example working. I am am pretty familiar with writing Windows C# apps but this is my first foray into Android. I have written an app which runs on Android (either emulator or tablet) by porting some C# code over from Windows. To add your code I created a C# class file in the folder ending in .Android and over wrote it with your supplied code. I also changed the manifest as you instructed. I cannot have set the tool up correctly as I have a red underline under the following words:

NetService
INetServices
WifiManager
Service

Mono.Android is in the references.

Any pointers as to what I am missing? If there are some example projects you can recommend I could take a look at them.

0 Votes 0 ·
Show more comments

Hi Leoon Lu,

wifiManager is now deprecated. Pls let me know which solution could be used.

Louis Mulder

0 Votes 0 ·