Android Marshmallow - How to avoid Wi-Fi drop if No Internet access - android

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;
}
}
}

Related

Android Connect to open WiFi - Fragmented between Nexus 5 and Nexus 5X

While using this to connect to an open WiFi network (that is not configured yet on the device):
public static void connectToWifiNetwork(Context context, final String ssid, String password) {
final WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.disconnect();
// Delete already available network
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
Log.i(TAG, "Deleting configuration for " + ssid);
wifiManager.removeNetwork(i.networkId);
break;
}
}
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Log.d(TAG, "Added network " + ssid + " " + password);
final int addNetworkResult = wifiManager.addNetwork(conf);
new Thread(new Runnable() {
#Override
public void run() {
Log.d(TAG, "Attempting to connect to " + ssid + " with id " + addNetworkResult);
wifiManager.enableNetwork(addNetworkResult, true);
}
}).start();
}
On Nexus 5 with API 23 (6.0.1), the added network has result -1, does not connect.
On Nexus 5X with API 26 (8.0.0), the added network has result 2, connects fine.
I am building for target API 25.
I am not sure if it is about the API level or the device, but I'd like to have a solution to rule them all.
Any ideas?
Edit:
Also tried with ALL the configurations as in this SO question:
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
conf.allowedAuthAlgorithms.clear();
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);
Didn't work on Nexus 5 as well.
Note: I can connect to WEP/WPA/WPA2 programmatically using both devices by using this implementation.
For some reason, I had to manually delete the open network from Android's WiFi settings (even though connecting through it works fine). It now connects, but not every time.
It disconnects from current network, tries to connect to the open network, then connects to the previous network again.

WiFi connection to WPA2 PSK

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.

Android - connect to known hidden Wi-Fi network

I need to connect to hidden Wi-Fi network programmatically.
I know it's SSID, security type and password.
For some reason I can't connect it.
I can connect to the same network if it's not hidden.
Here's my code:
// configure the network
private void saveWPANetwork(WiFiNetwork network){
WifiConfiguration conf = new WifiConfiguration();
conf.SSID =network.getSSID();
conf.hiddenSSID = true;
conf.status = WifiConfiguration.Status.ENABLED;
conf.preSharedKey =network.getPassword();
conf.priority = 9999;
wifi.addNetwork(conf);
wifi.saveConfiguration();
}
// connect it
protected boolean connectToVaildNetwork() {
List<WifiConfiguration> list = wifi.getConfiguredNetworks();
if(list == null)
return false;
for( WifiConfiguration i : list ) {
for (WiFiNetwork network : config.wiFiNetworksDetails) {
if(network.getSSID().equalsIgnoreCase(i.SSID)){
wifi.enableNetwork(i.networkId, true);
return wifi.reconnect(); /// STRANGE BUT IT ALWAYS RETURNS TRUE, EVEN IF DEVICE IS NOT CONNECTED TO THE HIDDEN NETWORK!
}
}
}
return false;
}
This answer maybe late but I still post it in case of someone need.
Android 4.4.2 tested. Notes that hidden networks take longer time to connect (for my test it was around 10-15s)
wifi.reconnect() == true means that your command has been requested successfully, it does not mean that wifi is connected.
public void setWifiConfig(String ssid, String sharedKey) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\""; // Please note the quotes. String should contain ssid in quotes
conf.preSharedKey = "\"" + sharedKey + "\"";
conf.hiddenSSID = true;
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);
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
wifiManager.saveConfiguration();
break;
}
}
}

How to connect to wifi with a specific bssid android?

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);

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