web-dev-qa-db-fra.com

Android obtient l'adresse IP d'un appareil fournissant un hotspot

J'utilise actuellement

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

pour obtenir l'adresse IP des périphériques en cours d'exécution. Cela fonctionne bien si l'appareil est connecté à un réseau wlan "commun" ainsi que l'appareil est connecté à un réseau wifi qui est hébergé par un autre appareil Android via hotspot. Si l'appareil n'est pas connecté à un réseau wifi "0.0.0.0" est renvoyé (correct). Mais si l'appareil héberge un réseau wifi en fournissant un hotspot, la méthode retourne toujours "0.0.0.0". Comment puis-je obtenir la véritable IP- Adresse d'un hotspot fournissant un appareil "dans son propre réseau wifi"?

merci et salutations

16
user2224350

Vous avez presque raison, l'adresse IP par défaut du hotspot est 192.168.43.1 (Si le fabricant de l'appareil n'a pas changé.)

Vous pouvez vérifier le code source de Android framework (AOSP).

/frameworks/base/services/Java/com/Android/server/connectivity/Tethering.Java/frameworks/base/wifi/Java/Android/net/wifi/WifiStateMachine.Java

Dans le 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",
};

Aussi, dans le 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;                                                                             
}   

Par conséquent, la valeur par défaut est 192.168.43.1.

25
Seongeun So

J'ai testé quelques petits appareils différents et il semble que l'appareil fournissant le hotspot ait toujours l'IP 192.168.43.1 sur son réseau. Quelqu'un peut-il vérifier/confirmer cette hypothèse?

12
user2224350

Le Hotspot agit probablement comme un serveur DHCP. donc,

Pour obtenir l'adresse IP (serveur) du hotspot wifi après s'être connecté à la méthode d'appel depuis la télécommande (client)

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

ensuite

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();
    }
}

renverra l'adresse IP du point d'accès connecté, et oui, la plupart des adresses IP par défaut du point d'accès sont 192.168.43.1

5
Pratik Tank

Bien que ce soit une vieille question, mais cela pourrait aider quelqu'un. Cela renverra l'adresse IP de votre appareil, tant que vous avez activé le hotspot.

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;
}
2
Mehedi Hasan

J'ai également vérifié plusieurs appareils, tous les appareils ont la même IP qui est 192.168.43.1, vous pouvez essayer cette adresse, mais dans Android pie, elle devient 192.168.43.68

0
Siva Krishna

ouvrir termux et exécuter

pkg install net-tools
ifconfig -a | grep 192.168.43.

vous obtiendrez ceci:

        inet 192.168.43.248  netmask 255.255.255.0  broadcast 192.168.43.255
0
Ray Foss