3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
a.b.d
  • 2,190
  • 3
  • 26
  • 26
  • It depends on the phone, for me it's eth0, you can run netcfg command via adb to display a network interface list. – a.b.d May 12 '11 at 21:02
  • [How to get the wifi network interface name in java](http://stackoverflow.com/questions/4507628/how-to-get-the-wifi-network-interface-name-in-java) is related to this question but not an exact duplicate. Here I asked specifically about Android and not how to do it in plain Java. The other question is worth reading, but I think you should use specific Android API when they are available as they should be more reliable. – a.b.d Sep 10 '13 at 13:14
  • @a.b.d Were you able to figure out that from all the list of interfaces, how would you differentiate the one for the Wi-Fi ? Would your solution also work when user is offline? – Shobhit Puri Jan 10 '17 at 00:01

6 Answers6

4

All you have to do is change

Log.e("MyTemp",netInterface.getName());

to

Log.e("MyTemp",netInterface.getDisplayName());
robquad
  • 41
  • 2
2

Try this

for(Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();)
    {
            NetworkInterface i = list.nextElement();
            Log.e("network_interfaces", "display name " + i.getDisplayName());
    }
angadsg
  • 184
  • 1
  • 3
  • 7
1

Use this: String interfaceName = SystemInfo.getInstance().getProperty("wifi.interface");

This will definitely work..

Harshit Syal
  • 649
  • 2
  • 11
  • 22
  • This is not available on Android. – bk138 Nov 23 '12 at 17:54
  • The value is available via getprop. I'd be interested to know common this is across all devices though. The advantage of this method is that it works when Wi-Fi is off, unlike all the other answers which have their root in what Chloe said. – Rob Pridham Apr 12 '13 at 12:41
0

Try this:

ls /sys/class/ieee80211/*/device/net/

Non root, tested on Android 4, Android 7.1, Android 9 and Arch Linux.

VasileM
  • 606
  • 7
  • 13
0

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).

Mrudul Tora
  • 715
  • 8
  • 14
0

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
Chloe
  • 25,162
  • 40
  • 190
  • 357