24

I'm currently using

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

to get the IP-Address of the executing devices. That works fine if the device is connected to a "common" wlan-network as well as the device is connected to a wifi network which is hosted by an other android device via hotspot. If the device is not connected to any wifi network "0.0.0.0" is returned (correct). But if the device is hosting a wifi network by providing a hotspot the methode is still returning "0.0.0.0". How can I get the real IP-Address of a hotspot providing device "in its own wifi-network"?

thx & regards

user2224350
  • 2,262
  • 5
  • 28
  • 54

6 Answers6

35

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

Therefore, the default value is 192.168.43.1 .

Seongeun So
  • 807
  • 1
  • 10
  • 17
  • 1
    @Gaucho Yes it can't solve the question, but there's no official API for this problem. Even though using reflection technique, it's almost impossible to access that method on almost all device. Therefore, we need to choose the most possible hard-coded IP-address. – Seongeun So Jan 16 '15 at 02:27
  • 2
    @SeongeunSo The default value is working fine in motoG device, but nexus it varies. Can you pls tell me that how to evaluate this ipaddress of wifi hotspot ? – Ramesh Akula Feb 27 '15 at 05:26
  • 1
    @RameshAkula did you find the answer? i am stuck in the same problem moto e working fine but nexus it varies . – Rohit Singh Apr 11 '17 at 21:02
18

I tested a small couple of different devices and it seems that the hotspot providing device has always the IP 192.168.43.1 on its network. Can somebody please check/confirm this assumption?

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
user2224350
  • 2,262
  • 5
  • 28
  • 54
  • 1
    Yes the default IP address is the one that you mentioned and also please check [this comment](http://android.stackexchange.com/questions/111443/transferring-continuous-android-camera-feed-to-pc-through-usb-in-a-background-a/111451?noredirect=1#comment145017_111451) – Lucky Jul 27 '15 at 17:04
  • 1
    Yes it's the default IP address for the hotspot providing Android device. – Mutai Mwiti May 10 '17 at 13:10
  • 1
    Yes i checked with several devices. default ip is perfect. – Vrajesh Jan 12 '18 at 13:12
  • Yes, The default ip is the same as mentioned by you. – AMT Apr 12 '20 at 07:00
  • 1
    No, for me it always keeps changing. It is of the form 192.168.43.* – Lokesh Aug 13 '20 at 10:11
  • Before Android 9 the IP address was hard-coded. But afterward, it is randomized. Source: [this answer](https://android.stackexchange.com/a/214072/367775) – s.ouchene May 23 '22 at 07:10
10

Though this is an old question, but this might help someone. This will return the ip address of your device, as long as you've turned on the 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;
}
Mehedi Hasan
  • 101
  • 1
  • 3
  • This solution works. I am now using this in [my app](https://github.com/umer0586/AndroidSMSServer/blob/master/app/src/main/java/github/umer0586/smsserver/util/IpUtil.java) with little amendments – Umer Farooq Dec 05 '22 at 18:45
3

The Hotspot likely acts as a DHCP server. so,

To get IP address (server) of wifi hotspot after getting connected to it call method from remote (client)

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

then

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

will return ip address of connected hotspot, and yes most default IP address of hotspot is 192.168.43.1

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Pratik Tank
  • 2,213
  • 1
  • 17
  • 29
2

open termux and run

ip -4 route get 8.8.8.8 | grep via

You'll something like this:

8.8.8.8 via 192.168.43.248 dev wlan0 table 1030 src 192.168.43.20 uid 12345
Ray Foss
  • 3,649
  • 3
  • 30
  • 31
  • No need to install net-tools to use ifconfig command.. but still +1ed for using termux :) – Krishna Oct 09 '20 at 16:04
  • @सत्यमेवजयते good point... ifconfig may be removed from future versions of android though. I don't see iproute2 being removed though... updated it to use iproute2. Man I love android. – Ray Foss Oct 10 '20 at 02:04
  • This was really useful. Thanks – imperial-lord Jan 06 '22 at 02:35
0

I was also checked several number of devices all the devices have same ip that is 192.168.43.1 you can try this address but in android pie it becomes 192.168.43.68