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).
Related
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.
I have to show list of access points (with no internet) and then connect to a selected network. I was able to get all available network network with wifiManager.getScanResults() then i have filtered my preferred open network and configured with wifiManager.addNetwork(conf);then i am trying to connect that preferred network with wifiManager.enableNetwork(j.networkId, true) It connecting to That network and again switching back to previous network.
1) the selected network gets connected
2) then its disconnected after couple of seconds
3 )wifi connects to the previously connected network
The behavior is only observed in Android devices with OS above 7.0. Marsh mellow and below device it is not switching network
here is my code
1 ) scanning and configuring
if (netType == ConnectivityManager.TYPE_WIFI) {
//wifiManager =
(WifiManager)this.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results = wifiManager.getScanResults();
if (!results.equals(null)) {
for (int i = 0; i < results.size(); i++) {
ScanResult result = results.get(i);
if(result.SSID.toLowerCase().startsWith("XXXX.")) {
list.add(result.SSID);
conf =new WifiConfiguration();
conf.SSID = "\"" + result.SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
}
}
}
2) to connect preferred network
if (j.SSID != null && j.SSID.equals("\"" + XXXXXX+ "\"")) {
Log.d("Config", "" + j.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(j.networkId, true);
wifiManager.reconnect();
break;
}
I'm trying to connect to a WiFi hotspot (open network) which doesn't have Internet access when the app starts.
However, there's another saved Wifi which has Internet. When I turn on Wifi, it always connects automatically to the one with Internet access.
I have been trying to fix this issue for like a week now! But nothing is working. In fact, my code disconnects from the network with internet but doesn't connect to the network I want. It doesn't make sense.
On the WiFi settings activity, it says "No Internet detected. Won't reconnect automatically."
private boolean tryConnect(WifiManager wifiManager, List<ScanResult> scanResults) {
for (ScanResult scanResult : scanResults) {
Log.d(TAG, "SCAN-RESULT: " + scanResult);
if (scanResult.SSID.toLowerCase().contains(MainActivity.ARDRONE2_HOTSPOT_NAME) && WifiUtilities.getScanResultSecurity(scanResult) == WifiUtilities.NetworkSecurity.OPEN) {
Log.d(TAG, "Trying Connecting to ARDrone2");
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = String.format("\"%s\"", scanResult.SSID);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.priority = Integer.MAX_VALUE - 1;
List<WifiConfiguration> wifiConfigList = wifiManager.getConfiguredNetworks();
int networkId = -1;
for (WifiConfiguration wifiConfig : wifiConfigList) {
if (wifiConfig != null) {
if (wifiConfig.SSID.equals("\"" + scanResult.SSID + "\"")) {
networkId = wifiConfig.networkId;
} else {
wifiManager.disableNetwork(wifiConfig.networkId);
}
}
}
if (networkId == -1) {
networkId = wifiManager.addNetwork(wifiConfiguration);
} else {
networkId = wifiManager.updateNetwork(wifiConfiguration);;
}
wifiManager.saveConfiguration();
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
return true;
}
}
return false;
}
The debug log: "Trying Connecting to ARDrone2" appears and yet it doesn not connect!!!
A similar question has been asked here Android, automatically connecting to wifi networks that have no internet access but no comments or answers were provided.
It seems like the problem was with my own phone. I have CM13.0 and apparently they don't allow connecting to networks that don't have Internet access. Even manual connecting to a network without Internet is a lot of trouble.
I tried it on other phones with non-custom ROMs and they all worked as expected according to the code above:
Disable all saved networks (this could be improved)
Disconnect from currently connected network
Connect to the ARDrone2
I've been working on wifi connecting for a while. I can use an Android device to open wifi hotspot and another Android device connect to it.
but here is a problem. I found out the wifi state will disconnect and reconnect to a wifi it already remembered and the last (I guess when it found the hotspot is ready) disconnect again and reconnect to my wifi hotspot. this process took almost 20~30 seconds.
here is my code:
private void connectToWiFiHotSpot() {
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
mWifiConfig = new WifiConfiguration();
mWifiConfig.SSID = "\"test5678\"";
mWifiConfig.preSharedKey = "\"12345678\"";
mWifiConfig.priority = 100000;
int res = wifiManager.addNetwork(mWifiConfig);
Log.d("WifiPreference", "add Network returned " + res);
wifiManager.disconnect();
boolean isEnable = wifiManager.enableNetwork(res, true);
Log.d("WifiPreference", "enable Network returned " + isEnable);
wifiManager.reconnect();
}
my question is there any way to let my device connect to my hotspot directly without connecting to what it remembered. speed up and increase performance.
I've already tried set a high priority and remove other remember wifi configurations is not what i want.
Any help I will be appreciated.
OK, I found a solution.
To disable all other configured networks before we add a specified wifi configuration.
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
//disable others
for (WifiConfiguration wifiConfiguration: wifiManager.getConfiguredNetworks()) {
wifiManager.disableNetwork(wifiConfiguration.networkId);
}
mWifiConfig = new WifiConfiguration();
mWifiConfig.SSID = "\"test5678\"";
mWifiConfig.preSharedKey = "\"12345678\"";
mWifiConfig.priority = 100000;
int res = wifiManager.addNetwork(mWifiConfig);
Log.d("WifiPreference", "add Network returned " + res);
wifiManager.disconnect();
boolean isEnable = wifiManager.enableNetwork(res, true);
Log.d("WifiPreference", "enable Network returned " + isEnable);
wifiManager.reconnect();
it will directly connect to wifi you assigned instead connect to other wifi it remembered first.
and in the end, after you disconnect to that wifi. Don't forget to enable other wifi.
it will connect to wifi it had connected to.
for (WifiConfiguration config: wifiManager.getConfiguredNetworks()) {
wifiManager.enableNetwork(config.networkId, true);
}
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()