Search code examples
androidandroid-wifi

Getting the IP Address of an Android Device using it's hotspot from another android device


I have two android enabled devices.On one device i turned on the hotspot and from other device i am connecting to that hotspot. Now i want to get the IP address of the first device. How can i get it. Because i want to send a file to the first device that's why i need the IP address of that device.I am assuming WifiManager will be used for this purpose but i don't know how. I read some other threads that are using NetworkInterface for this purpose.

-Usman


Solution

  • 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())
          return inetAddress.getHostAddress().toString(); 
       }
     }
    
    }
     catch (SocketException ex) 
     { 
       Log.e("ServerActivity", ex.toString());
      }