5

I am writing an Android application that needs to connect to different Wifi networks based on the user's selection. I need to retrieve the gateway IP address from the networkInfo. The Problem I am facing is that if I am connected to wifi network configuration A, and then want to switch to network configuration B, the wifi.getDhcpInfo(); returns to gateway IP address of network A. After several tries through the User interface workflow, it eventually returns the gateway IP of network B. Code snipet is below. Any ideas how to determine when the newly enabled network will return accurate Dhcp information so that I can get it reliably. Is there an ansynchronous event that I can catch for example, etc. Thanks.

WifiConfiguration config = wifiConfiguredNetworks.get(SSID);
enableNetworkResult = false;
enableNetworkResult = wifi.enableNetwork(config.networkId,true);
if (enableNetworkResult == true) {
    this.networkInfo = wifi.getDhcpInfo(); // does not return proper IP info    
    this.DeviceIP = android.text.format.Formatter.formatIpAddress(networkInfo.gateway);
}
Matthias Ronge
  • 9,403
  • 7
  • 47
  • 63
user1157108
  • 51
  • 1
  • 3

2 Answers2

2

I have exactly same issue and able to fix it with workaround. Just need to create worker thread with checking wifiManager.getConnectionInfo().getIpAddress() == 0 Something like this:

final Handler h = new Handler();
final WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
new Thread(new Runnable() {
    @Override
    public void run() {
        while (wifiManager.getConnectionInfo().getIpAddress() == 0) {
            Log.d(TAG, "waiting for valid ip");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        h.post(new Runnable() {
            @Override
            public void run() {
                // proceed here
            }
        });
    }
}).start();

I also tried all possible listeners, receivers etc. Nothing helped. The only way to get valid dhcp info is to wait for not null ip address.

deviant
  • 3,539
  • 4
  • 32
  • 47
  • I tried some related approach (just without an extra thread), but this approach only works if you have no IP address configured at all beforehand. If you switch the network, the wifi manager continues to report the outdated DHCP configuration of the previous network for a long while and then suddanly reports the new one. – Matthias Ronge Jul 21 '15 at 07:57
-1

Try to catch WifiManager.WIFI_STATE_ENABLED while listening to WIFI_STATE_CHANGED event- this state will come after all connection procedures are finished, so gateway ip should be set properly at this stage.

this should go to your onResume function:

IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
this.registerReceiver(networkStateListener, filter);

this - to onPause

this.unregisterReceiver(networkStateListener);

and this is receiver itself

BroadcastReceiver networkStateListener = new  BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(BroadcastReceiver.class.getSimpleName(), "action: "
                + intent.getAction());
        int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,-1);
        isNetworkAvailable =state == WifiManager.WIFI_STATE_ENABLED;
        // here you can get gateway address
    }

};
  • I haven't tested this solution, it's just a suggestion, so if it does not working let me know please
Raiv
  • 5,731
  • 1
  • 33
  • 51
  • It does not work. When switching from one wifi to another, `WIFI_STATE_ENABLED` is received even before supplicant state changes to `COMPLETED`. And even after that `getDhcpInfo()` still returns the values of the wifi just disconnected from. Only after some seconds it returns the appropriate values. – Matthias Ronge Jul 21 '15 at 08:29