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

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

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

how do i add network in wificonfig after API level26

i am facing a problem with wifi if the ap has same ssid and password i am getting BSSID as null or any from getconfiguredNetwork() method. so, i want to add network manually into wifi configuration but this add,remove and update will not work from api levl 26. is there any other alternative way to solve this solution. Reference Link.
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.BSSID = Bssid;
conf.preSharedKey = "\"" + networkPasskey + "\"";
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.BSSID != null && i.BSSID.equals(Bssid)) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reassociate();
Log.d("changing network", "connecting the right network");
break;
}
}
You will require location permissions, starting with Android 8.1 and 9 Google changed how you can access WiFi info.
https://developer.android.com/about/versions/pie/android-9.0-changes-all#restricted_access_to_wi-fi_location_and_connection_information
You will need ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions as well as rely on the user having their location service enabled. This cannot be done any other way because of privacy issues relating to WiFi.

How to scan Wi-fi networks continuously until find specific one?

I just started an app that must connect automatically to an open access point called "DEVICE AP".
I'm already getting a list with wifi networks, but I don't get "DEVICE AP" until I try the scan for three times (more or less).
So, my questions is: How can I scan continuously until I find "DEVICE AP"?
And other one, is it correct to connect to my open network by this way:
private void connectDeviceAccessPoint() {
String networkSSID = "DEVICE AP";
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + networkSSID + "\"";
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(wifiConfig);
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();
//Start service to send messages to device.
startService(new Intent(WifiActivity.this, SocketService.class));
doBindService();
break;
}
}
}
Thanks in advance!

Where are methods responsible for showing WiFi networks' options?

I'm going to create xposed module that will add one option to the window which appears when user clicks/holds one of WiFi networks. The dialog box where are located options like "connect", "modify", "cancel".
Where do I find these methods?
public void connectTowifi()
{
String ssid = ""// ssid of clicked network
String pass = "" // pass of clicked network
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" ssid +"\"";
wifiConfig.preSharedKey = "\"" + OfflineUtils.generatePassword(ssid) + "\"";
wifiManager.addNetwork(wifiConfig);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration wifiConfiguration : list )
{
if(wifiConfiguration!=null && wifiConfiguration.SSID != null && wifiConfiguration.SSID.equals(wifiConfig.SSID))
{
wifiManager.disconnect();
wifiManager.enableNetwork(wifiConfiguration.networkId, true);
wifiManager.reconnect();
}
}
}
Similarly for diabling network use
http://developer.android.com/reference/android/net/wifi/WifiManager.html#disableNetwork(int)
for forgetiing network
http://developer.android.com/reference/android/net/wifi/WifiManager.html#removeNetwork(int)
for disconnect connection:
http://developer.android.com/reference/android/net/wifi/WifiManager.html#disconnect()
For updating netwrok
http://developer.android.com/reference/android/net/wifi/WifiManager.html#updateNetwork(android.net.wifi.WifiConfiguration)
For all other methods
refer to
http://developer.android.com/reference/android/net/wifi/WifiManager.html

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