获取在Android WiFi接口名称 [英] Get Wifi Interface name on Android

查看:799
本文介绍了获取在Android WiFi接口名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/4507628/how-to-get-the-wifi-network-interface-name-in-java">How取得的Java WiFi网络接口名称

Possible Duplicate:
How to get the wifi network interface name in java

我目前正在开发一种无线网络嗅探器。为了实现这个目标我使用为ARM编译一个tcpdump的二进制文件。但它假定我知道WiFi接口的名称。

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.

据SDK文档的NetworkInterface 提供一个getName()方法。照片 我打算使用这种方法,所以第一步是获得相当于我的WiFi接口的NetworkInterface OBJET。
要做到这一点我用WifiInfo来获取IP联系地址,然后得到一个InetAddress对应于这个IP最后得到的NetworkInterface实例通过静态方法getByInetAddress(InetAddress类地址)。

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

下面是我的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());

输出:

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

我也尝试了isVirtual()方法,但它不编译,我得到一个错误信息,说方法isVirtual()没有定义类型的NetworkInterface。 我不明白是怎么回事...
任何帮助将AP preciate。

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.

推荐答案

试试这个

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

这篇关于获取在Android WiFi接口名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆