在Android上获取Wifi接口名称

问题描述 投票:3回答:5

我目前正在开发一种wifi嗅探器。为此,我使用为arm编译的tcpdump二进制文件。但是假设我知道Wifi接口的名称。

根据SDK文档NetworkInterface,提供了一个getName()方法。我计划使用此方法,因此第一步是获取与我的wifi接口相对应的NetworkInterface对象。为此,我使用WifiInfo获取ip地址,然后获取与该IP对应的InetAddress,最后使用静态方法getByInetAddress(InetAddress address)获取NetworkInterface的实例。

这是我的代码:

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());

输出:

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

但是我除外:

eth0

我也尝试过isVirtual()方法,但是它没有编译,并且我收到一条错误消息,提示未为NetworkInterface类型定义isVirtual()方法。我不明白发生了什么...任何帮助将不胜感激。

android android-networking
5个回答
1
投票

尝试一下

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

3
投票

您要做的就是改变

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

to

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

1
投票

使用此:字符串interfaceName = SystemInfo.getInstance()。getProperty(“ wifi.interface”);

这肯定会工作..


0
投票

您可以调用/system/ip link并解析结果。

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

0
投票

尝试一下:

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

非根,在Android 4,Android 7.1,Android 9和Arch Linux上进行测试。

© www.soinside.com 2019 - 2024. All rights reserved.