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")}
}
Related
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.
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;
}
}
I tried to create an application for wifi connection. After running some piece of code, i can't even connect my phone to the wifi. I have this piece of code
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", "XXX");
wifiConfig.preSharedKey = String.format("\"%s\"", "xxxxxxxxxxx");
WifiManager wifiManager = (WifiManager)fragmentActivity.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
The method addNetWork() always returns -1. I'd like to know what piece of code I have to write to reset all wifi settings to find my wifi back.
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!
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()