Reputation: 2220
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.
According to the SDK documentation NetworkInterface provide a getName() method.
I plan to use this method, so the first step is to get the NetworkInterface objet corresponding to my wifi interface.
To do that I use the WifiInfo to get the ip adress, then get an InetAddress corresponding to this IP and finally get an instance of NetworkInterface by using the static method getByInetAddress(InetAddress address).
Here is my code :
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
InetAddress addr = InetAddress.getByAddress(bytes);
NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
Log.e("MyTemp",netInterface.getName());
The output :
SSID: Nancy-Universite, BSSID: 00:19:30:6a:a9:40, MAC: B4:07:F9:D5:7C:8C, Supplicant state: COMPLETED, RSSI: -80, Link speed: 11, Net ID: 6
But I except something like :
eth0
I also try the isVirtual() method but it doesn't compile, and I get an error message saying the method isVirtual() is not define for the type NetworkInterface.
I don't understand what is going on...
Any help will be appreciate.
Upvotes: 3
Views: 27838
Reputation: 753
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).
Upvotes: 0
Reputation: 626
Try this:
ls /sys/class/ieee80211/*/device/net/
Non root, tested on Android 4, Android 7.1, Android 9 and Arch Linux.
Upvotes: 0
Reputation: 659
Use this: String interfaceName = SystemInfo.getInstance().getProperty("wifi.interface");
This will definitely work..
Upvotes: 1
Reputation: 26264
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
Upvotes: 0
Reputation: 41
All you have to do is change
Log.e("MyTemp",netInterface.getName());
to
Log.e("MyTemp",netInterface.getDisplayName());
Upvotes: 4
Reputation: 184
Try this
for(Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();)
{
NetworkInterface i = list.nextElement();
Log.e("network_interfaces", "display name " + i.getDisplayName());
}
Upvotes: 2