Get Wifi Interface name on Android

巧了我就是萌 提交于 2019-12-19 04:02:02

问题


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.


回答1:


Try this

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



回答2:


All you have to do is change

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

to

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



回答3:


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

This will definitely work..




回答4:


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



回答5:


Try this:

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

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



来源:https://stackoverflow.com/questions/5980826/get-wifi-interface-name-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!