2

I want to resolve next problem. My device is in AP mode (portable WiFi hotspot). It has to show IP of it. Another device connect to this one by using known IP. It have to work without any WiFi-routers, just device to device. How to get the IP address if the radio is already running in AP mode? I have some code about AP:

public boolean setWifiApEnabled(WifiConfiguration config, boolean enabled) {         
       try {
        if (enabled) { // disable WiFi in any case
         mWifiManager.setWifiEnabled(false);
        }

        Method method = mWifiManager.getClass().getMethod(
          "setWifiApEnabled", WifiConfiguration.class,
          boolean.class);
        return (Boolean) method.invoke(mWifiManager, config, enabled);
       } catch (Exception e) {
        //Log.e(TAG, "", e);
        return false;
       }
      }

public int getWifiApState() {
       try {
        Method method = mWifiManager.getClass().getMethod(
          "getWifiApState");
        return (Integer) method.invoke(mWifiManager);
       } catch (Exception e) {
        //Log.e(TAG, "", e);
        return WIFI_AP_STATE_FAILED;
       }
      }

 public static boolean IsWifiApEnabled(Context context){ 
          boolean isWifiAPEnabled = false;        
          WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          Method[] wmMethods = wifi.getClass().getDeclaredMethods();
          for(Method method: wmMethods){
              if(method.getName().equals("isWifiApEnabled")) {  
                  try {
                    isWifiAPEnabled = (Boolean) method.invoke(wifi);
                  } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                  } catch (IllegalAccessException e) {
                    e.printStackTrace();
                  } catch (InvocationTargetException e) {
                    e.printStackTrace();
                  }
              }
          }
          return isWifiAPEnabled;
      }
 }

Maybe there is some trick to get IP of it? Please help me. Thanks.

ulmo
  • 83
  • 8
Nolesh
  • 6,848
  • 12
  • 75
  • 112

2 Answers2

1

I use this to determine the IP address:

    private String determineHostAddress() {
        try {
            for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) {
                NetworkInterface ni = nis.nextElement();
                if (!ni.isLoopback() && !ni.isVirtual() && ni.isUp() && !ni.isPointToPoint() && ni.getHardwareAddress() != null) {
                    for (Enumeration<InetAddress> ips = ni.getInetAddresses(); ips.hasMoreElements(); ) {
                        InetAddress ip = ips.nextElement();
                        if (ip.getAddress().length == 4) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception ignored) {}
        return null;
    }
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
-1

This may not apply in your case but in my application I was planning to display the IP address of the hotspot so that I could enter it on another Android device that was connecting to the hotspot to connect to a webserver running on the hotspot. In that scenario the client (the device connected to the hotspot) can simply query the ip address of the gateway that it is connected to once it is connected to the hotspot. This will always be the ip address of the hotspot. Here is my code:

@SuppressWarnings("deprecation") // Deprecated because it doesn't handle IPv6 but wifimanager only supports IPV4 anyway
private String getGateway()
{
    final WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    return Formatter.formatIpAddress(dhcpInfo.gateway);
}

By the way, on every Android I have tested on so far the ip address of the hotspot is always 192.168.43.1. According to this question it is hardcoded in Android source.

Community
  • 1
  • 1
Josh
  • 1,277
  • 12
  • 21