Connecting to a specific Wi-Fi network programmatically - android

Here's a link where the answer present!
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
He is doing disconnect() before enabling particular SSID every time.
I followed the same and it is working fine for me.
But the question is why every time disconnect() needed before enable ?

Don't use wifiManager.disconnect(). If needed connect to API 19-25. If you need the highest level 8-8.1 you must use it. You can try this library.
But it has one bug - phone keeps the connection for just 35 seconds and after this, it returns to default wifi access point.

Related

Programmatically connecting to a specific wifi

I would like to connect to specific wifi programmatically in android. I referred lot of links. But still not yet achieve the goal. I am following below code to connect specific wifi.
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = String.format("\"%s\"", "ssid");
configuration.preSharedKey = String.format("\"%s\"", "paasword");
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
int netId = wifiManager.addNetwork(configuration);
wifiManager.disconnect();
wifiManager.enableNetwork(netId,true);
wifiManager.reconnect();
When I was try to run this code stuff.. wifi connection will be disconnected and again automatically connected previous wifi network.I am using redmi 6a mobile(version 9) for testing purpose.
Can any one guide me how to resolve this one.
Thanks in advance.
I believe you need to loop through all available networks, then connect to right one if it is available.
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();
break;
}else{Log.e("TAG","Network Not Available")}
}

Android O WifiManager.enableNetwork() cannot work

Fist of all, I manually set up two wifi with my phone, then I use WifiManager.enableNetwork() I can successfully switch wifi, but after a few seconds, wifi switches back to the previous wifi, what is the reason?
please tell me.
Device: ASUS ZenFone 5 (Android 8.0.0)
Code:
void connectWiFi(String networkSSID) {
int networkId = getNetworkId(networkSSID);
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
}
int getNetworkId(String ssid) {
List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
for (WifiConfiguration config : configurations) {
if (config.SSID != null && config.SSID.equals("\"" + ssid + "\"")) {
return config.networkId;
}
}
return -1;
}
I found the answer.
First, use the broadcast to listen to WiFi disconnect, then use wifiManager.disconnect(), wait for the broadcast to disconnect, and then use wifiManager.enableNetwork(networkId, true).

Android, automatically connecting to wifi networks that have no internet access

I'm calling the function WifiManager.addNetwork(WifiConfiguration) to try and add an adhoc wifi network to the device's wifi configurations. But on Android M this function returns -1, I'm assuming because it doesn't have internet access. It works fine on most other devices. Below is code snippet I'm using.
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = '\"' + ssid + '\"';
wifiConfiguration.hiddenSSID = false;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(wifiConfiguration);
Any way to get around the internet connectivity check and force the network to be added?
You need to enable the network after that :
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();
enter code here
break;
}
}

Connect to specific network, disable WiFi if the network doesn't exist (My issue in the second part)

I'm trying to add connect to specific network, I've made an Editbox, where the user enter the network he wannna connect to, after pressing done, it will save it in SharedPreference, and i will get the string and store it.
CODE DELETED, USE THE CODE IN THE ANSWER .
If your initial case is successfully connecting to the network if it does exist, then you could have a Boolean and check that after the while loop to see if you need to disable the wifi. Something like:
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
boolean connected = false;
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equalsIgnoreCase("\"" + DesiredSSID + "\"")) {
Log.d("In", "In!");
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
connected = true;
break;
}
if(!connected){wifiManager.setWifiEnabled(false);}

WifiManager.getConfiguredNetworks always returns empty list

I am trying to connect to an open wifi network. When I open my app it should turn on wifi and connect to the network defined as below. The problem is that WifiManager.getConfiguredNetworks always returns me an empty list. I have tried using locks too without success.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.priority = Integer.MAX_VALUE;
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if(list.isEmpty())
{
Log.e("Connection Setup","Empty list returned");
}
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
Log.e("Connection Setup",i.SSID+" connrction attempted");
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
I have been trying more things - if I make this thread sleep for about 10 seconds or so - everything works fine - but is there a better alternative?
You can not continue the
CMD wifiManager.getConfiguredNetworks()
until the status of WiFi is enabled completely. For enabling the WiFi it need some time. So you need to delay some time.
Try to comment these lines in your code.. do you see any change in result set?
wifiManager.setWifiEnabled(true);
wifiManager.addNetwork(conf);
You might want to check the return for wifiManager.addNetwork(conf);, is it returning -1.
For me this lines return 31 objects only.. no idea why..still hunting for it.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> arraylist = wifiManager.getConfiguredNetworks();
Log.wtf("WifiPreference","No of Networks"+arraylist.size());
I also met this question unfortunely.
After searching for some time, I think it is a bug.
This is the android implement of getConfiguredNetworks
public List<WifiConfiguration> getConfiguredNetworks() {
try {
return mService.getConfiguredNetworks();
} catch (RemoteException e) {
return null;
}
}
It is clearly shown that the function will return null if RemoteException happened when running. Up to now, I am also distressed with this and could not get some points to address this issue.
For more information:
https://code.google.com/p/android/issues/detail?id=19078
Since new Android versions (I think since Android 12) you also need to add COARSE_LOCATION permission to your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Then you can use (Android <= 28):
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
For Android >= 29:
wifiManager.getScanResults()

Categories

Resources