I am trying to create a wi-fi connection from a scan result. The only advertised capability is ESS and it is a network with no security details.
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = result.SSID;
wc.BSSID = result.BSSID;
//No password. it should be an open network
wc.status = WifiConfiguration.Status.ENABLED;
wc.priority = 100000;
wc.hiddenSSID = false;
int netId = mainWifi.addNetwork(wc);
if (netId == -1)
{
showMessageDialog("Error connecting to network.");
return;
}
mainWifi.enableNetwork(netId, true);
mainWifi.setWifiEnabled(true);
I keep getting -1, which is completely unhelpful and neither the console or logcat is giving me any output on this.
Am I missing something? Is there a way to debug this problem?
The SSID must be in Quotes:
wc.SSID = "\"SSID_NAME\""; //IMP! This should be in Quotes!!
Answer comes from this question:
https://stackoverflow.com/a/8818921/178931
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + result.SSID + "\"";
wc.BSSID = "\"" + result.BSSID + "\"";
Related
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.
I'm writing an app that connects to an hotspot with no internet connection.
It seems that on some Nexus devices with Android Marshmallow, when you connect to a network with no internet requests, the system "drops" the wifi connection and all request are not sent to the hotspot connection.
After a few seconds, a notification pops up asking if you want to stay connected to it. When the answer is "Yes", requests are sent to the hotspot successfully and you're able to get responses.
I've encountered the following thread:
https://android.stackexchange.com/questions/131132/force-marshmallow-to-keep-a-wi-fi-without-internet-access
However, I'm looking for a programmatically way (for not rooted devices) to tackle this problem.
I'm currently connecting to Wifi networks with the following code:
public void connectTo(String capabilities, String password, String ssid) {
WifiManager mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\"";
if (capabilities.contains("WPA2")) {
conf.preSharedKey = "\"" + password + "\"";
} else if (capabilities.contains("WPA")) {
conf.preSharedKey = "\"" + password + "\"";
} else if (capabilities.contains("WEP")) {
conf.wepKeys[0] = "\"" + password + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
mWifiManager.addNetwork(conf);
List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
if (list == null) {
return;
}
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
mWifiManager.disconnect();
mWifiManager.enableNetwork(i.networkId, true);
mWifiManager.reconnect();
break;
}
}
}
I am struggling through one issue that is- I am trying to connect with a SSID which is WPA2 PSK by below code:
WifiConfiguration conf = new WifiConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
conf.SSID =bed_ssid_name;
} else {
conf.SSID = "\"" + bed_ssid_name + "\"";
}
conf.preSharedKey = "\""+ networkPass +"\"";
wifi.addNetwork(conf);
List<WifiConfiguration> list = null;
list = wifi.getConfiguredNetworks();
//wifi.disconnect();
//wifi.disconnect();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + bed_ssid_name + "\"")) {
System.out.println("!!!!!!!### several ssid "+i.SSID +" ssid "+"\""+bed_ssid_name+"\"");
wifi.enableNetwork(i.networkId, true);
break;
}
}
wifi.reconnect();
Above code is working fine upto KitKat. But i am facing some inconsistency issue in Lollipop. In Goggle nexus and Motorola 2nd gen some time its not connected and some its connected successfully but after some time spontaneous it get disconnect. Then after some Google i found some more permission we need to give then i have done this
public void addWifiConfig(String ssid,String password) {
// Log.d(TAG, "Inside addWifiConfig...");
if (ssid == null) {
throw new IllegalArgumentException(
"Required parameters can not be NULL #");
}
String wifiName = ssid;
WifiConfiguration conf = new WifiConfiguration();
// On devices with version Kitkat and below, We need to send SSID name
// with double quotes. On devices with version Lollipop, We need to send
// SSID name without double quotes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
conf.SSID = wifiName;
} else {
conf.SSID = Constants.BACKSLASH + wifiName + Constants.BACKSLASH;
}
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
// conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
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.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int newNetworkId = wifi.addNetwork(conf);
wifi.enableNetwork(newNetworkId, true);
wifi.saveConfiguration();
wifi.setWifiEnabled(true);
}
But still same issue, Please get me rid of this issue. What i am missing here.
I am creating an Android app which allows to connect you to a WiFi with a specific BSSID. I have implemented the part which scan all the wifis and make the WiFi configuration in function of WiFi network security type. I have also implemented the connection to WiFi network with a specific BSSID.
But I have a problem: the connection to a specific BSSID works well for secure networks but doesn't work really for open network and I don't know why. Indeed this connection to an open network with a specific BSSID works on Samsung Galaxy S4 or more recent but doesn't work on Galaxy S2 and S3 ... It's really weird. On S2 and S3 the BSSID isn't take into account during connection.
This is the sample code that I use to create the configuration for a open network and try to connect on it with the specific BSSIDs:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + wifiItem.getSSID() + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) parentActivity.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
conf.BSSID = wifiItem.getBSSID();
wifiManager.updateNetwork(conf);
wifiManager.saveConfiguration();
for (WifiConfiguration wifiConfiguration : wifiManager.getConfiguredNetworks()) {
if (wifiConfiguration.SSID.equals("\"" + wifiItem.getSSID() + "\"")) {
wifiManager.disconnect();
wifiConfiguration.BSSID = wifiItem.getBSSID();
wifiManager.updateNetwork(wifiConfiguration);
wifiManager.enableNetwork(wifiConfiguration.networkId, true);
wifiManager.reconnect();
}
If someone could help me it would be so cool. I spent a lot of time on it and really don't understand this problem.
You can try looking this code over, this is what we use to connect to networks:
private void connectToNetwork(String password, ScanResult result, String capabilities) {
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + result.SSID + "\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
if (capabilities.contains("WPA2")) {
wc.preSharedKey = "\"" + password + "\"";
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
}
if (capabilities.contains("WPA")) {
wc.preSharedKey = "\"" + password + "\"";
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
}
if (capabilities.contains("WEP")) {
wc.wepKeys[0] = password;
wc.wepTxKeyIndex = 0;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
}
if (capabilities.contains("TKIP")) {
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
}
if (capabilities.contains("CCMP")) {
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.PairwiseCipher.CCMP);
}
if (!hasWifiSecurity(capabilities)) {
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
// wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
// // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
// wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res);
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b);
Log.d("", "Reassociate: " + wifi.reassociate());
Log.d("", "ReConnect: " + wifi.reconnect());
wifi.saveConfiguration();
WifiInfo connection = wifi.getConnectionInfo();
if (connection != null) {
if (connection.getSupplicantState().equals(SupplicantState.INACTIVE)) {
wifi.removeNetwork(res);
wifi.saveConfiguration();
scanForWiFiNetworks();
wifi.enableNetwork(wifi.getConnectionInfo().getNetworkId(), true);
}
}
// scanForWiFiNetworks();
// if (password.equalsIgnoreCase("")) {
// setScanningEnabled(true);
// }
// Toast.makeText(con, "Connecting to network: " + connectionInfo, Toast.LENGTH_SHORT).show(getMessageComments(dialog.getTextID()));
}
We need to connect to a 5G network with the same SSID as the 2.4G network, so the only way is to set the BSSID. i've just successfully connected to the several specified BSSID networks with the code below, have a try:
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
config.BSSID = BSSID; // <--BSSID should be set without ->"<- (-__-)b...
// remove this network from the configed(saved) network list
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs)
{
if (null != existingConfig && existingConfig.SSID.toString().equals("\"" + SSID + "\""))
{
mWifiManager.removeNetwork(existingConfig.networkId);
}
}
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//...
//...
// get the networkid
int wcgID = mWifiManager.addNetwork(config);
// enabled this network
boolean b = mWifiManager.enableNetwork(wcgID, true);
I am trying setup WiFi programatically on an Android tablet. If there is no WiFi connection then add the network with SSID and passkey read from a text file. Adding this network to network list and saving the passkey. It's adding the network and saving the passkey also but when I try to connect, it's not connecting. let me know...
public static void setupWifi(Context _context)
{
if(deviceConfig.wireless_ssid.length()==0) return;
WifiManager wifi = (WifiManager)_context.getSystemService(_context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + deviceConfig.wireless_ssid + "\""; //IMP! This should be in Quotes!!
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.priority = 40;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.preSharedKey = "\"" + deviceConfig.wireless_passkey + "\"";//
Log.d("ssid : ", wc.SSID );
List<WifiConfiguration> netWorkList = wifi.getConfiguredNetworks();
WifiConfiguration wifiCong = null;
if (netWorkList != null) {
for(WifiConfiguration item:netWorkList) {
if (item.SSID.equalsIgnoreCase("\"" + deviceConfig.wireless_ssid + "\"")) {
wifiCong = item;
}
}
}
if (wifiCong == null) {
boolean res1 = wifi.setWifiEnabled(true);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b );
boolean es = wifi.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es );
}
}
Thanks
Kiran
I had this problem as well. The way I solved it was manually adding the wifi network to the device. I then enumerated over the configuration and copied the values that the device added to successfully connect to that network.
To clarify:
Add the network by hand, call the network manager to get the configured networks, find your network, and then call the getters out of everything you are trying to set - you will find the exact configuration you need.
void getWifiConfigs()
{
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> networks = wifi.getConfiguredNetworks();
for (WifiConfiguration current : networks){
//check getters here
}
}