Android: Temporarily connect to WiFi network? - android

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

Related

WiFiManager EnableNetwork() not working on android 10

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.

WifiManager enableNetwork reverts to previous network after a few seconds

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.

wifiManager addNetwork() succeeds but network not visible under Wi-Fi Settings

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

failed to connect to specific WiFi in android programmatically

I'm using the following code to detect and connect to specific WiFi ssid when I press a button in android. Below is the code. Any help will be appreciated.
ssid :- "myHotspot" & password:- "12345678"
Button b1 = (Button) findViewById(R.id.button); <br>
b1.setOnClickListener(new View.OnClickListener() {
<br><br>#Override
<br>public void onClick(View v) {
wifiConfiguration.SSID = "\"myHotspot\"";
wifiConfiguration.preSharedKey ="\"12345678\"";
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
int netId = wifiManager.updateNetwork(wifiConfiguration);
if (wifiManager.isWifiEnabled()) { //---wifi is turned on---
//---disconnect it first---
wifiManager.disconnect();
} else { //---wifi is turned off---
//---turn on wifi---
wifiManager.setWifiEnabled(true);
}
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}
});
The main problem I'm getting is that my phone gets connected to the ssid and after 2-3 seconds it loses the connection and gets connected to my home Wifi router (which has internet connectivity)
Note:- The ssid I'm trying to connect is just a local hotspot without any internet connection.
and if I try with "addNetwork(wifiConfiguration)" it creates multiple networks of the same name. so Now how do I resolve this ?!
I think the problem here is you try to enableNetwork immediately after the call to wifiManager.setWifiEnabled(true). Generally, switching on wifi will take 5-10 seconds depending on the device, until then any call to wifiManager.enableNetwork will be lost. Hence, your call to connect to the desired network is getting lost and as soon as the wifi is switched on, your device connects to the last network it remembers.
Try to create a loop where you keep checking if wifiManager.isWifiEnabled() == true and keep looping until it returns true (with Thread.sleep() obviously and doing this in an AsyncTask or separate Thread). Only after that try to call enableNetwork.

Android WifiManager making a phantom connection?

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.

Categories

Resources