在安卓系统上获取Wifi接口名称[英] Get Wifi Interface name on Android

本文是小编为大家收集整理的关于在安卓系统上获取Wifi接口名称的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我目前正在开发一种wifi嗅探器.实现我使用用于臂的TCPDump二进制文件.但它假设我知道WiFi接口的名称.

根据SDK文档 networkinterface 提供getname()方法.
我计划使用此方法,因此第一步是获取与我的WiFi接口对应的网络接口Objet.
为此,我使用wifiinfo获取IP地址,然后获取与此IP对应的inetaddress,最后通过使用静态方法getByInetAddress(inetaddress地址)获取网络接口的实例.

这是我的代码:

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()方法,但它不编译,我收到错误消息,说方法是isvirtual()没有定义类型网络接口. 我不明白发生了什么......
任何帮助都会欣赏.

推荐答案

试试这个

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

其他推荐答案

您所要做的就是更改

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

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

其他推荐答案

使用它: 字符串interfaceName = systeminfo.getInstance().getProperty("wifi.interface");

这绝对是工作..

本文地址:https://www.itbaoku.cn/post/102415.html

问题描述

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.

推荐答案

Try this

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

其他推荐答案

All you have to do is change

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

to

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

其他推荐答案

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

This will definitely work..