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.