22

我目前正在使用

public static String getLocalIPAddress(WifiManager wm){
    return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}

获取执行设备的 IP 地址。如果设备连接到“通用”wlan-network 并且设备连接到由其他 android 设备通过热点托管的 wifi 网络,则效果很好。如果设备未连接到任何 wifi 网络,则返回“0.0.0.0”(正确)。但是,如果设备通过提供热点来托管 wifi 网络,则方法仍然返回“0.0.0.0”。如何“在自己的 wifi 网络中”获取热点提供设备的真实 IP 地址?

谢谢和问候

4

6 回答 6

34

你几乎是对的,热点的默认IP地址是192.168.43.1(如果设备制造商没有改变。)

您可以查看 Android 框架 (AOSP) 的源代码。

/frameworks/base/services/java/com/android/server/connectivity/Tethering.java /frameworks/base/wifi/java/android/net/wifi/WifiStateMachine.java

在 Tethering.java 中,

private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
private static final int USB_PREFIX_LENGTH        = 24;

// USB is  192.168.42.1 and 255.255.255.0
// Wifi is 192.168.43.1 and 255.255.255.0
// BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
// with 255.255.255.0

private String[] mDhcpRange;
private static final String[] DHCP_DEFAULT_RANGE = {
    "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
    "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
    "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
    "192.168.48.2", "192.168.48.254",
};

此外,在 WifiStateMachine.java

private boolean startTethering(ArrayList<String> available) {                                 

    boolean wifiAvailable = false;                                                            

    checkAndSetConnectivityInstance();                                                        

    String[] wifiRegexs = mCm.getTetherableWifiRegexs();                                      

    for (String intf : available) {                                                           
        for (String regex : wifiRegexs) {                                                     
            if (intf.matches(regex)) {                                                        

                InterfaceConfiguration ifcg = null;                                           
                try {                                                                         
                    ifcg = mNwService.getInterfaceConfig(intf);                               
                    if (ifcg != null) {                                                       
                        /* IP/netmask: 192.168.43.1/255.255.255.0 */                          
                        ifcg.setLinkAddress(new LinkAddress(                                  
                                NetworkUtils.numericToInetAddress("192.168.43.1"), 24));      
                        ifcg.setInterfaceUp();                                                

                        mNwService.setInterfaceConfig(intf, ifcg);                            
                    }                                                                         
                } catch (Exception e) {                                                       
                    loge("Error configuring interface " + intf + ", :" + e);                  
                    return false;                                                             
                }                                                                             

                if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {           
                    loge("Error tethering on " + intf);                                       
                    return false;                                                             
                }                                                                             
                mTetherInterfaceName = intf;                                                  
                return true;                                                                  
            }                                                                                 
        }                                                                                     
    }                                                                                         
    // We found no interfaces to tether                                                       
    return false;                                                                             
}   

因此,默认值为192.168.43.1

于 2014-01-10T11:42:50.577 回答
17

我测试了几个不同的设备,似乎热点提供设备192.168.43.1在其网络上始终具有 IP。有人可以检查/确认这个假设吗?

于 2013-06-30T09:56:50.307 回答
6

虽然这是一个老问题,但这可能会对某人有所帮助。只要您打开了热点,这将返回您设备的 IP 地址。

private String getIpAddress() {
    String ip = "";
    try {
        Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (enumNetworkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = enumNetworkInterfaces
                    .nextElement();
            Enumeration<InetAddress> enumInetAddress = networkInterface
                    .getInetAddresses();
            while (enumInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enumInetAddress.nextElement();

                if (inetAddress.isSiteLocalAddress()) {
                    ip += "SiteLocalAddress: "
                            + inetAddress.getHostAddress() + "\n";
                }
            }
        }

    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        ip += "Something Wrong! " + e.toString() + "\n";
    }
    return ip;
}
于 2019-04-21T09:58:49.920 回答
3

热点可能充当 DHCP 服务器。所以,

连接到wifi热点后获取IP地址(服务器)从远程(客户端)调用方法

intToInetAddress(wifiManager.getDhcpInfo().serverAddress);// get hotspot ip

然后

public InetAddress intToInetAddress(int hostAddress) 
{
    byte[] addressBytes = {(byte) (0xff & hostAddress),
            (byte) (0xff & (hostAddress >> 8)),
            (byte) (0xff & (hostAddress >> 16)),
            (byte) (0xff & (hostAddress >> 24))};

    try 
    {
        return InetAddress.getByAddress(addressBytes);
    } 
    catch (UnknownHostException e) 
    {
        throw new AssertionError();
    }
}

将返回连接热点的IP地址,是的,热点的大多数默认IP地址是192.168.43.1

于 2016-09-20T11:08:55.113 回答
2

打开 termux 并运行

ip -4 route get 8.8.8.8 | grep via

你会是这样的:

8.8.8.8 via 192.168.43.248 dev wlan0 table 1030 src 192.168.43.20 uid 12345
于 2019-09-21T00:12:22.433 回答
0

我还检查了几个设备,所有设备都具有相同的 ip,即 192.168.43.1 你可以试试这个地址,但在 android pie 中它变成 192.168.43.68

于 2019-07-18T07:17:10.520 回答