Question by
nolash · Sep 27, 2016 at 10:38 AM ·
c#androidnetworking
Determine Wifi adapter address on Android
This piece of (crude) code is able to find my Wifi NetworkInterface
on my Windows 7 x64:
class WifiAddrTest {
IPAddress wifi_ipv4;
void getIP() {
foreach (NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces()) {
if (netif.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) {
IPInterfaceProperties properties = netif.GetIPProperties();
foreach (IPAddressInformation unicast in properties.UnicastAddresses) {
Debug.Log("sockettest");
if (unicast.Address.AddressFamily == AddressFamily.InterNetwork) {
Debug.Log("Using IP " + unicast.Address.ToString());
wifi_ipv4 = unicast.Address;
}
}
}
}
}
}
However when building this for Android through Unity3D, there are complaints in the adb log, and the ports I've tried to open with the auto-detected address aren't open:
D/STATUSBAR-NetworkController( 2141): refreshSignalCluster: data=-1 bt=false
I/Unity (12461): EntryPointNotFoundException: getifaddrs
I/Unity (12461): at (wrapper managed-to-native) System.Net.NetworkInformation.LinuxNetworkInterface:getifaddrs (intptr&)
I/Unity (12461): at System.Net.NetworkInformation.LinuxNetworkInterface.ImplGetAllNetworkInterfaces () [0x00006] in /Users/builduser/buildslave/mono/build/mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs:249
I/Unity (12461): at System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces () [0x00053] in /Users/builduser/buildslave/mono/build/mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs:72
I/Unity (12461):
I/Unity (12461): (Filename: /Users/builduser/buildslave/mono/build/mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs Line: 249)
I/Unity (12461):
I/Unity (12461): NullReferenceException: Object reference not set to an instance of an object
I/Unity (12461): at Sockettest.Update () [0x00033] in C:\Users\Public\Documents\Unity Projects\New Unity Project\Assets\Sockettest.cs:62
If I hardcode the IPAddress
, it works.
wifi_ipv4 = IPAddress.Parse("192.168.0.1");
Any thoughts on how to get the Wifi IP addr dynamically?
The Android device is Samsung Galaxy S3 Mini, OS version 4.1.2. Unity version 5.4.1
Comment