Android change wifi network not working - android

I am trying to connect to a specific wifi network, as described here. However, while sometimes it works, most of the times it doesn't. I do not really understand why if I run the app, let's say 10 times, the device connects to my network 2 times, and the other 8 it doesn't.
In the following is the code I use in a Fragment.
WifiConfiguration wificonfiguration = new WifiConfiguration();
wificonfiguration.SSID = "\"" + networkSSID + "\"";
wificonfiguration.priority = 40;
wificonfiguration.status = WifiConfiguration.Status.ENABLED;
// WPA management
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wificonfiguration.preSharedKey = "\"" + networkPass + "\"";
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
int networkId = wifiManager.addNetwork(wificonfiguration);
if (networkId > -1) {
boolean status = wifiManager.enableNetwork(networkId, true);
}
The network configuration is actually added to the manager and status value is true. I have run it on several devices running Lollipop. Also, the network I want to connect to does not provide internet access, so I also thought that may be a problem for what stated in the note here. Can anybody tell me if the code is ok?
Solved
The problem was solved by adding wifiManager.disconnect(); before adding the new network configuration.

You could disable all the network via below described method before adding the network. This would disable all the configured networks to auto-connect.
for (WifiConfiguration wifiConfiguration_ : wifiManager.getConfiguredNetworks()) {
wifiConfiguration_.status = WifiConfiguration.Status.DISABLED;
int returnNetworkId_ = wifiManager.updateNetwork(wifiConfiguration_);
}

Related

how do i add network in wificonfig after API level26

i am facing a problem with wifi if the ap has same ssid and password i am getting BSSID as null or any from getconfiguredNetwork() method. so, i want to add network manually into wifi configuration but this add,remove and update will not work from api levl 26. is there any other alternative way to solve this solution. Reference Link.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.BSSID = Bssid;
conf.preSharedKey = "\"" + networkPasskey + "\"";
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.BSSID != null && i.BSSID.equals(Bssid)) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reassociate();
Log.d("changing network", "connecting the right network");
break;
}
}
You will require location permissions, starting with Android 8.1 and 9 Google changed how you can access WiFi info.
https://developer.android.com/about/versions/pie/android-9.0-changes-all#restricted_access_to_wi-fi_location_and_connection_information
You will need ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions as well as rely on the user having their location service enabled. This cannot be done any other way because of privacy issues relating to WiFi.

connect wpa2 enterprise wifi connection programmatically in android

I just tried few codes for wpa2 enterprise connection in android but nothing is connecting i want a right code to connect the right network. right now i have used this answer but i need few clarification because this answer is very old one. here i am attaching some screenshot about connection clarification.
In this you can see identity and password
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.BSSID = Bssid;
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.DISABLED;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.enterpriseConfig.setIdentity(identity);
wifiConfiguration.enterpriseConfig.setPassword(password);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
if (networkPasskey.matches("^[0-9a-fA-F]+$")) {
wifiConfiguration.wepKeys[0] = networkPasskey;
} else {
wifiConfiguration.wepKeys[0] = "\"".concat(networkPasskey).concat("\"");
}
wifiConfiguration.wepTxKeyIndex = 0;
i have found enterprice function in wificonfiguration to set identity and password.
wifiConfiguration.enterpriseConfig.setIdentity(identity);
wifiConfiguration.enterpriseConfig.setPassword(password);
but what is the use of this one. when we have identity and password.
if (networkPasskey.matches("^[0-9a-fA-F]+$")) {
wifiConfiguration.wepKeys[0] = networkPasskey;
} else {
wifiConfiguration.wepKeys[0] = "\"".concat(networkPasskey).concat("\"");
}
wifiConfiguration.wepTxKeyIndex = 0;
i am using BSSID because my AP have same ssid so i want to connect the right network by using BSSID
I have solved it by the help of this link
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + networkSSID + "\"";
config.BSSID = Bssid;
config.priority = 1;
String networkIdentity = "";
networkPasskey = "";
config.status = WifiConfiguration.Status.ENABLED;
WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
enterpriseConfig.setIdentity(networkIdentity);
enterpriseConfig.setPassword(networkPasskey);
enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2);
config.enterpriseConfig = enterpriseConfig;
WifiManager myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int id = myWifiManager.addNetwork(config);
Log.d("addNetwork", "# addNetwork returned " + id);
myWifiManager.enableNetwork(id, true);
but this method will work till android 7.0 in android 8.0 they have restricted many function we can not add wifi configuration manually.
three things we must know when you are trying to configure with wpa2 enterprise
you must know the EAP method it can be anything but mostly they will use PEAP
2.you must know about the Phase2 method, mostly they will use MSCHAPV2
Certificate this one maximum Do Not validate.

Is possible to programmatically connect to Wi-Fi network using Xamarin?

Using Xamarin.iOS and Xamarin.Android, it is possible to check if the Wi-Fi is enabled?
And, if is disabled, it's possible to enable it?
Once it's enabled, how to search and connect to a network?
Enable/Disable Wifi:
WifiManager wifiManager = (WifiManager)GetSystemService(WifiService);
if (!wifiManager.IsWifiEnabled)
wifiManager.SetWifiEnabled(true);
else
wifiManager.SetWifiEnabled(false);
Permission to change Wifi state:
android.permission.CHANGE_WIFI_STATE
Add a network:
var networkSSID = "Stack";
var networkPass = "Overflow";
var config = new WifiConfiguration();
config.Ssid = '"' + networkSSID + '"';
// For WPA/WPA2, WEP is different (still using WEP? shame on you ;-)
config.PreSharedKey = '"' + networkPass + '"';
wifiManager.AddNetwork(config);
Connect to a network:
IList<WifiConfiguration> myWifi = wifiManager.ConfiguredNetworks;
wifiManager.Disconnect();
wifiManager.EnableNetwork(myWifi.FindFirst(x => x.Ssid.Contains(networkSSID)), true);
wifiManager.Reconnect();

Network id is -1 for latest versions in android

I have created an application to connect to selected Wifi from the list. This works fine in the android version of 4.1.2(network id = 10) but if I try to run the same application on version 4.4.2 and 4.4.4 it shows the network id value as -1.
My code to achieve this is :
private void connectToWifi2() {
// TODO Auto-generated method stub
// Main method. Write code here.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = String.format("\"%s\"", selWifi.SSID);
conf.preSharedKey = String.format("\"%s\"", pwd.getText().toString());
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
conf.hiddenSSID = true;
// remember id
int netId = wifiManager.addNetwork(conf);
Log.d("RMGWiFiConfig", "netId returned" + netId);
if (netId >= 0) {
Log.i("RMGWiFiConfig", "Connecting with " + selWifi.SSID);
wifiManager.disconnect();
Boolean statusInBoolean = wifiManager.enableNetwork(netId, true);
Log.d("RMGWiFiConfig", "Enabled network returned "
+ statusInBoolean);
Log.d("RMGWiFiConfig", "Reassociate : " + wifiManager.reassociate());
Log.d("RMGWiFiConfig", "Reconnect " + wifiManager.reconnect());
} else {
Log.d("RMGWifiConfig", "Failed to add network configuration");
}
}
I tried to refer the below links but not able to resolve the problem. Or may be I am unable to fit in the solution in the right way provided in the below links.Please let me know the changes I need to do w.r.t my code.
Can not connect another android device with wifi with android lollipop
Android WifiConfiguration shows -1 for ID. How can I fix it for SSID to be recognized?
Android 6.0 Cannot add WifiConfiguration if there is already another WifiConfiguration for that SSID
android wifiManager.addNetwork returns -1
WifiConfiguration enable network in Lollipop
Android 5.0 Lollipop and 4.4 KitKat ignores my WiFi network, enableNetwork() is useless
Please Help!
Thanks
Shruti
I can't find an obvious critical reason, but I have similar code working on Android 6.x. The differences I can find are:
wc.StatusField = WifiStatus.Disabled; (enableNetwork() will update this later)
wc.PreSharedKey = "\"" + pw + "\"";
at the end: wm.SaveConfiguration();
If I had to guess, I'd say it was the first one that's causing the failure.

How to check if password for WiFi network is correct in Android?

In my application I ask user to select WiFi network and to enter password (if needed). How can I check if password is correct?
Here is my code for connecting to WPA networks
conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
networkPass = input.getText().toString();
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if (list.size()>0)
{
for (WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
two option:
1) Use BroadcastReceiver like this.You receive an intent after your success connected. But you will not get one if the password is incorrect. So my suggestion is :
2) check it out later(like after 2s) use the Handler. Because associating and connecting to a WiFi network takes time and the actual task of connecting or associating with the network is done asynchronously in the background. So just check it out later( 1s is too short and 2 second is work for me. You might try other number).

Categories

Resources