如何获取Android中wifi热点的IP? [英] how to get the IP of the wifi hotspot in Android?

查看:1083
本文介绍了如何获取Android中wifi热点的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说的...当配置为热点时,我正在尝试获取wifi iface的IP.理想情况下,我想找到一种适用于所有手机的东西.

As the title says... I'm trying to be able to get the IP of the wifi iface when it is configured as hotspot. Ideally, I would like to find something that works for all the phones.

当然,从AP那里获取信息时,WifiManager毫无用处.

Of course, the WifiManager is useless when it comes to get info from the AP.

幸运的是,通过这样做,我已经能够获得所有接口的IP:

Luckily, I've been able to get the IPs of all the interfaces by doing this:

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    Log.d("IPs", inetAddress.getHostAddress() );
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

这段代码将打印所有接口的所有IP(包括Wifi热点).主要问题是我找不到识别WiFi接口的方法.这是一个问题,因为某些电话具有多个接口(WiMax等).到目前为止,这是我尝试过的:

This chunk of code will print all the IP of all the interfaces (Wifi hotspot included). The main problem is that I don't find a way to identify the WiFi interface. This is an issue since some phones have multiple interfaces (WiMax, etc). This is what I've tried so far:

  • 按wifi iface显示名称进行过滤:这不是一个好方法,因为显示名称从一台设备更改为另一台设备(wlan0,eth0,wl0.1等).
  • 按其mac地址过滤:几乎可以正常工作,但是在某些设备上,热点iface没有MAC地址(iface.getHardwareAddress()返回null)...所以不是有效的解决方案.

有什么建议吗?

推荐答案

这是我获取wifi热点IP的方法:

Here's what I did to get the wifi hotspot ip:

public String getWifiApIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            if (intf.getName().contains("wlan")) {
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && (inetAddress.getAddress().length == 4)) {
                        Log.d(TAG, inetAddress.getHostAddress());
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

这将为您提供任何 wifi设备的IP地址,这意味着它不仅用于热点.如果您连接到另一个wifi网络(这意味着您不在热点模式下),它将返回IP.

This will give you the IP address of any wifi device, which means it's not just for the hotspot. If you're connected to another wifi network (meaning you're not in hotspot mode), it'll return an IP.

您应该先检查是否处于AP模式.您可以为此使用此类: http://www.whitebyte.info/android/android-wifi-hotspot-manager-class

You should check if you are in AP mode first or not. You can use this class for that: http://www.whitebyte.info/android/android-wifi-hotspot-manager-class

这篇关于如何获取Android中wifi热点的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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