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.
我正在编写一个Android应用程序,需要根据用户的选择连接到不同的Wifi网络。我需要从networkInfo检索网关IP地址。我面临的问题是,如果我连接到wifi网络配置A,然后想切换到网络配置B,则wifi.getDhcpInfo();返回到网络A的网关IP地址。经过几次尝试通过用户界面工作流程,它最终返回网络B的网关IP。代码snipet在下面。任何想法如何确定新启用的网络何时将返回准确的Dhcp信息,以便我可以可靠地获取它。是否有我可以捕获的异步事件等等。谢谢。
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);
}
2 个解决方案
#1
1
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:
我有完全相同的问题,并能够解决它与解决方法。只需要创建工作线程并检查wifiManager.getConnectionInfo()。getIpAddress()== 0这样的事情:
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.
我也试过所有可能的听众,接收器等。没有任何帮助。获取有效dhcp信息的唯一方法是等待非空IP地址。
#2
-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.
尝试在收听WIFI_STATE_CHANGED事件时捕获WifiManager.WIFI_STATE_ENABLED-此状态将在所有连接过程完成后出现,因此应在此阶段正确设置网关ip。
this should go to your onResume
function:
这应该转到你的onResume函数:
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
this.registerReceiver(networkStateListener, filter);
this - to onPause
这 - 对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
- 我没有测试过这个解决方案,这只是一个建议,所以如果它不起作用请告诉我
#1
1
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:
我有完全相同的问题,并能够解决它与解决方法。只需要创建工作线程并检查wifiManager.getConnectionInfo()。getIpAddress()== 0这样的事情:
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.
我也试过所有可能的听众,接收器等。没有任何帮助。获取有效dhcp信息的唯一方法是等待非空IP地址。
#2
-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.
尝试在收听WIFI_STATE_CHANGED事件时捕获WifiManager.WIFI_STATE_ENABLED-此状态将在所有连接过程完成后出现,因此应在此阶段正确设置网关ip。
this should go to your onResume
function:
这应该转到你的onResume函数:
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
this.registerReceiver(networkStateListener, filter);
this - to onPause
这 - 对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
- 我没有测试过这个解决方案,这只是一个建议,所以如果它不起作用请告诉我