- Home /
Get the WiFi IP address of the device
Hi, I'm developing a game for Android and IOS platforms and I'm looking for an option to get the device WiFi IP address from the Unity Script. As mentioned in many places I used the following code to get the IP address but it doesn't have a way to specifically get the WiFi IP address of the device.
public string GetLocalIPv4()
{
return Dns.GetHostEntry(Dns.GetHostName())
.AddressList.First(
f => f.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
.ToString();
}
Is there a way to get the WiFi IP address of the device?
Answer by GetLitGames · Sep 04, 2020 at 06:40 AM
Probably just removing check for Ethernet below will give you just WIFI and then you can look at properties in the debugger to add an IF statement that makes sure you get the WIFI interface you wanted:
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.OperationalStatus == OperationalStatus.Up)
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip != null && ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
// My addresses
if (ni.Description.ToLower().Contains("vpn"))
{
foundVPN = true;
}
}
}
}
}
Hi SubtleTechnology, Thank you for the solution you have provided. The problem here is, in IOS all the NetworkInterfaceTypes are given as Ethernet. Wireless80211 is not there. So there it is not possible to distinguish IP address assigned to mobile data and WiFI
OK, I would just look at all the properties of the ni object in the debugger and see if there is anything you can use.
Your answer
Follow this Question
Related Questions
Cross platform multiplayer 3 Answers
Smartphone multiplayer - NAT problem? 2 Answers
Unity networking tutorial? 6 Answers
Can you use Unity to make multiplayer IOS/Android games? 1 Answer
Why can Android send commands via NetworkManager but iOS cannot? 0 Answers