I confirm that ACCESS_FINE_LOCATION and ACCESS_WIFI_STATE permission is enabled, and my app TargetSdkVersion is 30.
I switched from Wi-Fi A to another Wi-Fi B and the Wi-Fi B connection was successful. But the info.getssid of wifiManager.getConnectionInfo() is always Wi-Fi A information. When I kill app and reopen it, wifiManager.getConnectionInfo() does right and return Wi-Fi B info.
Please help me Why wifiManager.getConnectionInfo() does not as expected? And How to solve it?
I registered for WifiManager.NETWORK_STATE_CHANGED_ACTION broadcast, and then I got network info :
Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (parcelableExtra instanceof NetworkInfo) {
NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
NetworkInfo.State state = networkInfo.getState();
if (state == NetworkInfo.State.CONNECTED) {
if (isSameNetwork()) {
// do something..
}
}
}
public boolean isSameNetwork() {
wifiManager = (WifiManager)host.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
if (info != null) {
String ssid = info.getSSID();
if (ssid != null && TextUtils.equals(ssid, myssid)) {
// do something..
return true;
}
}
return false;
}
I need to determine if current connection is it as I expected.
I would like to determine if the current Wi Fi connection is my expected Wi Fi