Wifi.getDhcpInfo () in Android returns invalid IP gateway

I am writing an Android application that needs to connect to various Wifi networks based on user choice. I need to get the IP address of the gateway from networkInfo. The problem I am facing is that if I am connected to Wi-Fi network configuration A, and then want to switch to network configuration B, wifi.getDhcpInfo (); returns to the IP address of the gateway of network A. After several attempts through the user interface workflow, it ultimately returns the IP address of the gateway of network B. Below is a link to the snipet code. Any ideas how to determine when a newly included network will return accurate Dhcp information so that I can get it reliably. Is there a synchronous 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); } 
+4
source share
2 answers

I have exactly the same problem and I can fix it with a workaround. You just need to create a workflow with wifiManager.getConnectionInfo () validation. 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 reliable dhcp information is to wait for a non-null ip address.

+1
source

Try to catch WifiManager.WIFI_STATE_ENABLED while listening to the WIFI_STATE_CHANGED event - this state will appear after all connection procedures have been completed, so the ip gateway should be configured correctly at this point.

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 the 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 have not tested this solution, it is just a suggestion, so if it doesn’t work, please let me know.
-1
source

Source: https://habr.com/ru/post/1391702/


All Articles