I'm using WifiManager to test for the presence of a particular SSID and verify a given WPA password, but I'm getting a weird result.
The code looks like this:
WifiConfiguration wc = new WifiConfiguration();
// init ssid and password as Strings ...
wc.SSID = "\"" + ssid + "\"";
wc2.preSharedKey = "\"" + password + "\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
boolean b = wifi.enableNetwork(res, true);
The weird part is that enableNetwork() returns true, even if the wifi network with SSID is not online (?!). Note, however, that if the target Wifi network is present and the password is correct, the code successfully makes the connection.
The Android documentation says enableNetwork() "returns true if the operation succeeded." My questions are:
1) How can the network have been enabled if it's not even there?
2) Have I initialized the WifiConfiguration parameter, wc, incorrectly?
3) Is this the right way to make/test a connection to a Wifi network?
1) How can the network have been enabled if it's not even there?
The Android documentation says enableNetwork() "returns true if the operation succeeded."
They actually mean that the enabling operation has been successfully initiated, it has't crashed... This could return false if the wifi instance is not linked any more to the wifi supplicant.
2) Have I initialized the WifiConfiguration parameter, wc, incorrectly?
I am not sure, but it looks alright.
3) Is this the right way to make/test a connection to a Wifi network?
Before enabling the network, you should check if the network is actually available by using the startScan() method. (You will get a list of SSID in the callback).
You should also use a BroadcastReceiver to get the result from the Intent action NETWORK_STATE_CHANGED_ACTION, where you could check if your connection to the access point is successful or not.
Related
On android 10 mobile phone. I seldom engaged in an issue.
The Enablenetwork() function doesn't work.
Here is some code
WifiConfiguration Config = new WifiConfiguration();
Config.Ssid = "\"" + SSID + "\"";
temp = WiFi.AddNetwork(Config);
WiFi.SaveConfiguration();
bool Enable_Success = WiFi.EnableNetwork(temp, true);
On my device(Samsung s9, android 10)
The function just sometimes works. Sometimes not working.
When not working. I observe the wifi status of my phone.
The wifi just keep connecting to current connected SSID.
Not even tried to connected to target SSID. Just keep it's current status.
What would be the problem?
Thank you!
Maybe you can try disconnect other network first.
wifiManager.disconnect();// disconnect first
WifiConfiguration Config = new WifiConfiguration();
Config.Ssid = "\"" + SSID + "\"";
temp = WiFi.AddNetwork(Config);
WiFi.SaveConfiguration();
bool Enable_Success = WiFi.EnableNetwork(temp, true);
Assuming that the network is open (meaning without any password), then you need to explicitly say it in the configuration object.
Config.AllowedKeyManagement.Set((int)KeyManagementType.None);
Android 10 uses a different set of functions to connect to wifi than previous versions of Android. You will want to do something like this:
var connectivity_manager = GetSystemService(Context.ConnectivityService) as ConnectivityManager;
var wifi_network_specifier = (new WifiNetworkSpecifier.Builder()).SetSsid(ssid).Build();
var network_request = (new NetworkRequest.Builder()).AddTransportType(TransportType.Wifi)
.SetNetworkSpecifier(wifi_network_specifier).Build();
connectivity_manager.RequestNetwork(network_request, network_callback);
Note that this code assumes an open network. If you have a WPA network, you can add the password to the WifiNetworkSpecifier.Builder. Also, as far as I can tell, the new WifiNetworkSpecifier.Builder does not support WEP networks. So, if you are attempting to programmatically connect to a WEP network with Android 10, you may have a difficult time.
On Android 8.1.0 and above, I am coming up against a major issue.
When connecting to a network with no internet using enableNetwork, Android decides after a few seconds to put the device back on the previous network.
I have intentionally connected to the network desired and am planning to use bindToNetwork to ensure that all traffic goes through the network, however Android appears to be ignoring the bind and simply disconnecting very quickly afterwards.
I have seen a few permutations of this question being asked here, all with no replies, unfortunately.
I am creating an connecting to the network with the following code. I am able to see the network connecting.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = desiredSSID;
if (desiredNetwork.has("password")) {
conf.preSharedKey = String.format("\"%s\"", desiredNetwork.get("password"));
}
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int networkID = wifiManager.addNetwork(conf);
wifiManager.enableNetwork(networkID, true);
Is there an available option to disable this behaviour, as I intentionally connecting to a network with no internet available.
Thanks.
I was able to resolve this issue by removing the existing config from the configured wifi list. The network will not disconnect from a new config.
List<WifiConfiguration> wifiConfigurationList = wifiManager.getConfiguredNetworks();
for (WifiConfiguration removeConfig : wifiConfigurationList) {
if (removeConfig.SSID.equals(desiredSSID)) {
wifiManager.removeNetwork(removeConfig.networkId);
break;
}
}
Simply adding the network again, as above fixes the issue.
Been struggling with connecting to WiFi network in Android 5 and 6 for a while and other similar questions don't seem to work for me. I could get the same code working in Android 4.4.2
Adding the code snippet below.
String networkSSID = getSsid();
String networkPass = getNetworkPass();
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.priority = 40;
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.preSharedKey = "\""+ networkPass +"\"";
int value = mWifiManager.addNetwork(conf);
Log.i(TAG_CHECK, "Connecting to : " + value);
boolean enabled = mWifiManager.enableNetwork(value, true);
Log.i(TAG_CHECK, "enabled to : " + enabled);
mWifiManager.reconnect();
And here is what I noticed.
mWifiManager.addNetwork(conf)
returns some (+) integer with Android 5(phone) and Android 6(tab).
But both don't connect unless I open the wifi settings (I don't have to do anything and just landing there connects) or I manually turn off and turn on Wifi from the top bar to automatically connect.
The listeners to detect connection to the network are in tact for both the versions to precisely know when the connection get established - confirming the above behaviour. Have attached a pic of the permissions granted below.
Any pointers as to what I am missing?
EDIT: Upon digging into WifiManager class, looks like the Access Point remains in WIFI_AP_STATE_DISABLED state. I should also highlight that everything worked as expected while trying on a different Android M phone.
EDIT2
I have the following observations.
1. The issue so far is specific to 1 android 6.0.1 Samsung tablet and 1 android 5.0.2 Micromax phone. It works just fine on 3 other android 6.0.1 phones, 1 android N phone and Android 4.4.2 phone.
2. The access point ends up in wifi_ap_disabled state in the problematic cases consistently. Both addNetwork and enableNetwork calls are affirmative.
3. These access points are not that of a router wifi but that of other phones that broadcast. The problematic phones can programatically connect to wifi hotspots (setup manually and not in the programatic way as I would like to) without any issue.
4. Mobile data enabled/disabled state or wifi state with a different connected network doesn't change the dynamics for both working and problematic phones.
This makes me think that it is a combination of phones/tabs (and not OS) and the access point broadcast configuration. Do you think I should be playing around with some config parameters?
Edit 3 - Important Update
So the wifimanager is obtained like below
WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
I instantiate mWifiManager in a service (inside onCreate method) and use that later to scan and connect with inputs from front-end activity. While it doesn't work in the way as described earlier, using the same snippet seems to work when the enableNetwork call is done right in the onCreate method - working just as expected. It also works fine when called from front-end activity but only when I don't instantiate mWifiManager in the service.
Though I would expect only one reference to a system service - WifiManager i.e, I think the subsequent usage of that reference (or a different reference to WifiManager) gets a lower priority (or gets deprioritized by the previous reference) and hence doesn't get completely executed leaving the access point in disabled state and requiring manual intervention to help complete the execution by WifiManager and other wifi system services.
Also, I notice the same in Android 5.0.2 phone, the asynchronous enableNetwork does get executed, but it takes some time to execute it.
My questions
1. It is no more a question about the correctness of the code. Services in general have lesser priority compared to front-end threads So, is there is way I could prioritise the enableNetwork call so that it gets immediately executed?
2. What happens in case of multiple references to WifiManager?
I believe if you add a disconnect() it will do what you want. I've added a check here because addNetwork() can fail to add your network. For Android 6.0+, you can't add a network if it exists already but if this fails you can try getting the network id then connecting. (For instance, it will be there if added (and saved) if you re-install) I also do not add quotes to SSID in Lollipop (ver 21)+.
int value = mWifiManager.addNetwork(conf);
// need to check the result if successful
if (value == -1) {
return; // network not added successfully
}
Log.i(TAG_CHECK, "Connecting to : " + value);
mWifiManager.disconnect(); // add a disconnect
boolean enabled = mWifiManager.enableNetwork(value, true);
Log.i(TAG_CHECK, "enabled to : " + enabled);
mWifiManager.reconnect();
I have successfully added and connected to open and PSK networks programmatically in my app (tried on devices running 5.1 as well as 6.0). However when I am trying this for enterprise network it does not work. I see that addNetwork() succeeds (returns positive net id) but when I look under Settings->Wi-Fi I don't see the SSID like I do for the other SSIDs I added. Anyone know why this would be? If I search the WiFiConfiguration list programmatically it does find the SSID. Here is the code I use:
wifiConf = new WifiConfiguration();
wifiConf.SSID = "\"dot1x-test\"";
wifiConf.BSSID = "c4:e9:84:43:48:e8";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
wifiConf.enterpriseConfig.setIdentity("name");
wifiConf.enterpriseConfig.setPassword("testpassword");
wifiConf.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
wifiConf.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2);
}
netId = wifiMgr.addNetwork(wifiConf);
wifiMgr.disconnect();
wifiMgr.enableNetwork(netId, true);
wifiMgr.saveConfiguration();
wifiMgr.reconnect();
when I connect to an 8open) WLAN network using this code:
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.BSSID =result.BSSID; // BSSID of detected network
wifiConfig.priority = 1;
wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.status=WifiConfiguration.Status.ENABLED;
int netId = scanData.wifiManager.addNetwork(wifiConfig);
scanData.wifiManager.enableNetwork(netId, true);
...this WiFi connection is stored in list of preferred networks. So when I try that very often I get a really huge list of connections - and that's somethin I do not want.
So my question: how can I establish a connection to a WLAN network just temporarily and let Android not store it permanently? Or is there a way to remove such a network in case it is no longer needed?
Thanks!
When you disconnect from the network, just remove it from the stored list. See details here: In Android, How do you clear the WI-FI list of networks in Settings via Java?
You could search if the wifiConfig is already saved, then remove it before connect: like this:
List <WifiConfiguration> wifiConf = wifiManager.getConfiguredNetworks();
int netId;
for(WifiConfiguration wf: wifiConf){
if(wf.BSSID.equals(desiredBSSID)){
netId = wf.networkId;
wifiManager.removeNetwork(netId);
}
}