Android get IP-Address of a hotspot providing device

前端 未结 6 1368
半阙折子戏
半阙折子戏 2020-12-05 05:27

I\'m currently using

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


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 06:07

    You're almost right, the default IP address of hotspot is 192.168.43.1 (If device maker didn't change.)

    You can check the source code of Android framework (AOSP).

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

    In the 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",
    };
    

    Also, in the WifiStateMachine.java

    private boolean startTethering(ArrayList 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;                                                                             
    }   
    

    Therefore, the default value is 192.168.43.1 .

提交回复
热议问题