How to determine wifi network interface name in java?
4 Answers
Since you said that you are developing on Android use this:
WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
System.out.println(wifiInfo.getSSID());
This will return the name of your WIFI.
You have to add this permission to your manifest.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

- 23,674
- 33
- 131
- 170
Can you specify? Do you want the SSID of the connected network? Or the name of the network adapters? I provided an example to get the name of all network interfaces.
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements())
{
NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
System.out.println(networkInterface.getDisplayName());
}
If you only want to select the WLAN Interface, maybe you have to do this via JNI, otherwise you could check the name for occurrences of "wlan" (like eth0, eth1 and so on). I don't recommend to rely only on this naming convention.
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements())
{
NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
if(networkInterface.getName().startsWith("wlan")) {
System.out.println(networkInterface.getDisplayName());
}
}

- 11,181
- 18
- 74
- 102
-
From this list, how to identify if the interface is a wifi interface? – dheeps Dec 22 '10 at 11:27
-
I adjusted my answer, does this help? – Christopher Klewes Dec 22 '10 at 12:00
-
@dheeps Did you find a solution. How to get the name of wifi interface? – Shobhit Puri Jan 09 '17 at 23:53
If you're after the Wi-Fi interface and only the Wi-Fi interface, then parsing /proc/net/wireless is the way to go on Android:
public static String getInterfaceName() {
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/net/wireless")));
// skip header
reader.readLine();
reader.readLine();
// read first interesting line
String ifLine = reader.readLine();
reader.close();
// extract interface name
String ifName = ifLine.substring(0, ifLine.indexOf(':'));
return ifName.trim();
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}

- 3,033
- 1
- 34
- 28
Try:
NetworkInterface.getNetworkInterfaces();
which will return all the interfaces present on your machine.

- 266,786
- 75
- 396
- 414
-
4
-
-
1Yes.But I wish if there is anyway to find interface typr from native java Apis – dheeps Dec 22 '10 at 11:58