I am currently developing a sort of wifi sniffer. To achieve that I use a tcpdump binary compiled for arm. But it\'s assume that I know the name of the Wifi Interface.
You can call /system/ip link
and parse the results.
bash-3.2# ip link
ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: usb0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 6e:3d:7a:3a:62:ee brd ff:ff:ff:ff:ff:ff
3: usb1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether da:bb:3f:04:1b:cd brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop state DOWN
link/sit 0.0.0.0 brd 0.0.0.0
5: ip6tnl0: <NOARP> mtu 1460 qdisc noop state DOWN
link/tunnel6 :: brd ::
6: ifb0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN qlen 32
link/ether 76:3c:4e:23:cc:f8 brd ff:ff:ff:ff:ff:ff
7: ifb1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN qlen 32
link/ether 02:17:54:31:ff:bd brd ff:ff:ff:ff:ff:ff
8: tun: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
link/ether b2:b7:11:da:7c:6a brd ff:ff:ff:ff:ff:ff
16: tiwlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 100
link/ether 40:fc:89:e4:67:4c brd ff:ff:ff:ff:ff:ff
All the answers related to this question are not straight-forward. But you can achieve this by reflection. The code mentioned below will work even when there is no wifi connection.
String interfaceName=null;
try {
Method m = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
interFace = (String) m.invoke(null, "wifi.interface");
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Wifi Interface Name: " + interfaceName);
It is working on all the api levels (even when your targetsdk will be 30 inspite of some limitations added in reflection).
Try this:
ls /sys/class/ieee80211/*/device/net/
Non root, tested on Android 4, Android 7.1, Android 9 and Arch Linux.
Try this
for(Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();)
{
NetworkInterface i = list.nextElement();
Log.e("network_interfaces", "display name " + i.getDisplayName());
}
Use this: String interfaceName = SystemInfo.getInstance().getProperty("wifi.interface");
This will definitely work..
All you have to do is change
Log.e("MyTemp",netInterface.getName());
to
Log.e("MyTemp",netInterface.getDisplayName());