问题
I'm able to add network using following code
but not able to get status if its added or authentication
failed after Enable
Network
and reconnect
. Please help!!!
mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
removeNetwork();
Boolean isWifiConnected = false;
WifiConfiguration mWifiConf = new WifiConfiguration();
mWifiConf.SSID = "\"" + networkList.get(item_pos).SSID + "\"";
mWifiConf.hiddenSSID = true;
mWifiConf.status = WifiConfiguration.Status.ENABLED;
mWifiConf.priority = 1;
if(addWPA2Network(mWifiConf, etPassword.getText().toString())<0)
{
}
else
{
List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list)
{
if (i.SSID != null && i.SSID.equals("\"" + networkList.get(item_pos).SSID + "\""))
{
mWifiManager.disconnect();
isWifiConnected = mWifiManager.enableNetwork(i.networkId, true);
isWifiConnected = mWifiManager.reconnect();
break;
}
}
}
回答1:
Add the android permissions for network to the manifest file.
回答2:
Got my answer from this link: https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/wifi/WifiStatusTest.java
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
handleSupplicantStateChanged(
(SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE),
intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR),
intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, 0));
}
Use this handler to check if its connected successfully or not.
来源:https://stackoverflow.com/questions/16014819/network-status-is-missing