This is killing me and any help would be greatly appreciated.
I want to connect to an open network using wifi manager. The problem I am having is that the code claims connection to any network - even non-existing ones. Below is the entire code that gets executed and gets called with the SSID of a network. It does not matter what string you pass to it as the SSID of a network, even if no such network exists in any shape or form, the enableNetwork claims returns true, which I believe means it connected to the network.
What I need to do is to make sure I have a connection. So if I pass a network SSID that does not exist (for example, it is out of range) the API should return a failure when attempting to connect.
Any ideas/hints/suggestions would be greatly appreciated.
public boolean conto (String network){
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configs = null;
int inetId = -1;
// make sure there are no funny stuff in the config
configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
wifi.removeNetwork(config.networkId);
Log.d("********", "Removed Network: SSID=[" + config.SSID + "] and ID=[" + config.networkId + "]");
}
// Now add the network
wifiConfiguration.SSID = "\"" + network + "\"";
wifiConfiguration.hiddenSSID = false;
//wifiConfiguration.priority = 1;
//wifiConfiguration.networkId = 999;
inetId = wifi.addNetwork(wifiConfiguration);
if(inetId < 0) {
Log.d("********", "Could Not Add Network......... [" + wifiConfiguration.SSID + "]");
}
else {
Log.d("********", "Added Network......... [" + wifiConfiguration.SSID + "]");
// Lets be paranoid and double check the config file
Log.d("********", " +++++++++++++++++++++++++ This is what I have in Config File");
configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
Log.d("********", "In the Config file after add, SSID=[" + config.SSID + "], ID=[" + config.networkId + "]");
}
// Now Enable the network
boolean successConnected = wifi.enableNetwork(inetId, true);
//boolean successAssociated = wifi.reassociate(); This did not change the results
if(successConnected) {
Log.d("********", "Connected to......... [" + inetId + "]");
}
else {
Log.d("********", "Could Not Connect to......... [" + inetId + "]");
}
}
return false;
}
I think the problem is that your code is based on the assumption that connecting to a WiFi network is a synchronous process, which it is not. The method enableNetwork() does not block while the connection occurs and then return the resulting success or failure of the connection, it returns immediately and the result is simply whether the request properly reached the WiFi service and the supplicant.
If you want visibility into what is going on with the connection status of the device, you need to monitor the WifiManager. SUPPLICANT_STATE_CHANGED_ACTION and WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION broadcast Intent actions with a BroadcastReceiver. Once you initiate the connection process, these callbacks will tell you how things progress. For example, if you pass in a network that doesn't exist, the connection status should almost immediately go straight to DISCONNECTED. While a valid network will go through the process of associating, authenticating, and connecting. All these steps are enumerated through these broadcasts.
Related
Premise:
I'm currently work on an Android App (API level 23, Android 6.0) that connect with a device via Wi-Fi and uses UDP packets to communicate. I'm able to change the device Wi-Fi password using a particular command. This works fine.
Target:
What I'm tring to programmatically do is:
search Wi-Fi generated from the device
connect to the device
send the command to change the password
reconnect to the device using the new password
I'm able to connect the first time (steps 1,2,3) using code like this:
private void connect(String ssid, String password) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = String.format("\"%s\"", ssid);
conf.preSharedKey = String.format("\"%s\"", password);
netId = mWifiManager.addNetwork(conf);
mWifiManager.saveConfiguration();
mWifiManager.disconnect();
mWifiManager.enableNetwork(netId, true);
mWifiManager.reconnect();
}
Additional info:
In the Manifest file I declared these permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
The problem:
If I try to use the same method to connect after the change of the password, I'm not able to achive the connection because (I think) Android remembers the previous password.
If I try to use updateNetwork(conf) instead of addNetwork(conf), I don't notice any difference.
I've tried to remove or disable in some ways the saved network before try to connect again, but unsaccesfully.
mWifiManager.removeNetwork(netId)
returns false (I have no idea why it fails)
mWifiManager.disableNetwork(netId);
returns true but it appears to have no effects
If I use the Android settings to change password, all works fine... but I want to change the saved password programmatically.
Any help is very appreciated
After several attempts, I currently have no problems following the instructions I report below.
Before connecting to a Wi-Fi network it is important to check the list of previously saved networks. If there is no network with the same SSID it is possible to create a new configuration, otherwise it is necessary to modify the existing one.
public void connect(String ssid, String password) {
WifiConfiguration wifiConf = null;
WifiConfiguration savedConf = null;
//existing configured networks
List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
if(list!=null) {
for( WifiConfiguration i : list ) {
if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
Log.d(TAG, "existing network found: " + i.networkId + " " + i.SSID);
savedConf = i;
break;
}
}
}
if(savedConf!=null) {
Log.d(TAG, "coping existing configuration");
wifiConf = savedConf;
} else {
Log.d(TAG, "creating new configuration");
wifiConf = new WifiConfiguration();
}
wifiConf.SSID = String.format("\"%s\"", ssid);
wifiConf.preSharedKey = String.format("\"%s\"", password);
int netId;
if(savedConf!=null) {
netId = mWifiManager.updateNetwork(wifiConf);
Log.d(TAG, "configuration updated " + netId);
} else {
netId = mWifiManager.addNetwork(wifiConf);
Log.d(TAG, "configuration created " + netId);
}
mWifiManager.saveConfiguration();
mWifiManager.disconnect();
mWifiManager.enableNetwork(netId, true);
mWifiManager.reconnect();
}
Android after version 6 does not allow any application to modify Wi-Fi networks unless it has been created by the application itself. In addition, it does not allow adding Wi-Fi networks with the same name as others already configured previously. This is very curious because when you configure Wi-Fi networks manually from the settings, it does allow it.
After thinking for a long time about a possible solution it occurred to me that perhaps the Wifi network could be added with another name and once added, change it to the desired name. I have tried it and it works. It would be something like the following:
int networkId = -1;
// Find my Wifi
List<WifiConfiguration> configuredWifis = wifiManager.getConfiguredNetworks();
for(WifiConfiguration wifi : configuredWifis)
{
if(wifi.SSID != null && wifi.SSID.equals("\"" + WIFI_SSID + "\"") && wifi.priority == WIFI_PRIORITY)
{
networkId = wifi.networkId;
}
else
{
wifiManager.disableNetwork(wifi.networkId);
wifiManager.removeNetwork(wifi.networkId); // After android 6 it really does not remove the Wifi network
wifiManager.saveConfiguration();
}
}
if(networkId == -1)
{
// If my Wifi is not yet configured then add it
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + WIFI_SSID + "\"";
conf.preSharedKey = "\""+ WIFI_PASSW +"\"";
conf.priority = WIFI_PRIORITY;
networkId = wifiManager.addNetwork(conf);
if(networkId == -1) // This ocurs when a wifi with the same name is yet configured. After Android 6 is not possible modify it.
{
Random random = new Random(System.currentTimeMillis());
int randomInteger = random.nextInt(10000);
conf.SSID = "\"" + WIFI_SSID + randomInteger + "\"";
networkId = wifiManager.addNetwork(conf); // Add my wifi with another name
conf.SSID = "\"" + WIFI_SSID + "\"";
conf.networkId = networkId;
networkId = wifiManager.updateNetwork(conf); // After my wifi is added with another name, I change it to the desired name
}
}
// Connect to my wifi
if(wifiManager.getConnectionInfo().getNetworkId() != networkId)
{
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
}
I am creating a list of in-range wifis and show it to the user. I want to the user can select each of the items in the list and insert the password in order to connect to the selected SSID.
I wrote this method for wifi connection:
private WifiConfiguration wifiConf; /* WifiConfiguration object */
private WifiManager wifiMgr; /* WifiManager object */
private WifiInfo wifiInfo; /* WifiInfo object */
public boolean connectToSelectedNetwork(String networkSSID, String networkPassword, String securityType) {
int networkId;
int SecurityProtocol;
if (securityType.contains("WEP")) {
SecurityProtocol = 1;
Log.i(TAG, "Security: WEP");
} else if (securityType.contains("WPA2")) {
Log.i(TAG, "Security: WPA2");
SecurityProtocol = 2;
} else if (securityType.contains("WPA")) {
Log.i(TAG, "Security: WPA");
SecurityProtocol = 3;
} else {
Log.i(TAG, "Security: OPEN");
SecurityProtocol = 4;
}
// Clear wifi configuration variable
clearWifiConfig();
// Sets network SSID name on wifiConf
wifiConf.SSID = "\"" + networkSSID + "\"";
Log.i(TAG, "SSID Received: " + wifiConf.SSID);
switch (SecurityProtocol) {
// WEP "security".
case WEP:
wifiConf.wepKeys[0] = "\"" + networkPassword + "\"";
wifiConf.wepTxKeyIndex = 0;
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
break;
// WAP security. We have to set preSharedKey.
case WPA2:
wifiConf.preSharedKey = "\"" + networkPassword + "\"";
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
break;
case WPA:
wifiConf.preSharedKey = "\"" + networkPassword + "\"";
// Network without security.
case OPEN_NETWORK:
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
break;
}
// Add WiFi configuration to list of recognizable networks
if ((networkId = wifiMgr.addNetwork(wifiConf)) == -1) {
Log.i("TAG", "Failed to add network configuration!");
return false;
}
// Disconnect from current WiFi connection
if (!disconnectFromWifi()) {
Log.i("TAG", "Failed to disconnect from network!");
return false;
}
// Enable network to be connected
if (!wifiMgr.enableNetwork(networkId, true)) {
Log.i("TAG", "Failed to enable network!");
return false;
}
// Connect to network
if (!wifiMgr.reconnect()) {
Log.i("TAG", "Failed to connect!");
return false;
}
return true;
}
But when I call this function in order to connect to the selected wifi I always get false! I debugged it a lot of times and it goes into first if in this method And doesn't connect to the wifi.
Please help me with your answers.
Thank you.
addNetwork() returns -1 on failure.
One common reason for it to fail is when the network was previously configured by the user (through the wifi setting screen) or programmatically, via an app with different Unix User ID (i.e. UID). Recall that every app is associated with a UID.
If the same UID calls addNetwork() for the second time, it will be allowed to override the configuration and the function will succeed.
A similar related behavior is with regards to removing a configuration via the removeNetwork() API. Only the app with the UID that added the configuration can remove it.
This behavior makes sense since you wouldn't want a malicious (or just buggy) app, to override or remove configurations done by either the user or other apps.
So far so good, but there's a complication - when an app is reinstalled (read: uninstalled and then installed again), the OS will provide it with a different UID. Now the same app which previously added the configuration, following the reinstall, can no longer remove or update it. Ouch!
It would have been nice to allow the same application ID to be able to update/remove a network. Oh well.
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.
I plan to make an app that checks if a certain wifi network uses common password. I have a list of most common passwords, but the following function:
public boolean connectTo(String psKey){
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + dbw.getSsid() + "\"";
wc.preSharedKey = "\"" + psKey + "\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc = configureCapabs(wc, dbw.getCapability());
int res = wifi.addNetwork(wc);
Toast.makeText(this, "add Network returned " + res , Toast.LENGTH_SHORT).show();
boolean b = wifi.enableNetwork(res, true);
Toast.makeText(this, "enableNetwork returned " + b , Toast.LENGTH_SHORT).show();
if(!b) return false;
boolean fin = /*b ? wifi.reassociate() : */wifi.reconnect();
return fin;
}
it returns true, even if the password was incorrect. Is there a way of checking if the password that I tried to log in with was accepted or rejected?
You are using the reconnect() / reassociate() methods of WiFiManager to check weather or not the connection succeeded but the boolean value that they return means something else. The returned value only tells you about the result of STARTING a given operation. This is because associating and connecting to a WiFi network takes time. These methods however will return instantly and will not block. The actual task of connecting or associating with the network is done asynchronously in the background.
You can monitor the what is happening with the WiFi connection by listening to a specific system wide broadcast:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
You can find more info about this HERE - Check Torsten's answer.
As far as checking if the password was accepted or rejected by way of a return value, I don't actually know however I can think of two 'possible' alternate strategies that might net the same result?
Not 100% sure as I haven't done it before, however I wonder if Pinging the supplicant would work?
http://developer.android.com/reference/android/net/wifi/WifiManager.html#pingSupplicant()
Either that or checking if you have an IP address now?
http://developer.android.com/reference/android/net/wifi/WifiInfo.html
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
}
}