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);
}
Related
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")}
}
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).
My app is connecting to a given wifi connection. After connecting i am able to see in saved by app. But when I do wifi off and on restart wifi that information saved by app is lost and therefore my app can not delete that network as from marshmallow app can not delete wifi connection created by user or other apps.
How to retain saved by myapp information in wifi restart or system reboot?
WifiConfiguration conf = new WifiConfiguration();
Log.d("SSID", networkSSID[0].toString());
Log.d("Pass", networkPass[0].toString());
conf.SSID = "\"" + networkSSID[0].toString() + "\"";
conf.preSharedKey = "\"" + networkPass[0].toString() + "\"";
int netId;
//reconnect = false;
netId = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
Before https://imgur.com/a/mqpH3
After wifi restart https://imgur.com/a/foxGV
I am developing an Android App. The app will keep trying to connect to an dedicated WiFi AP when started. This is working. However when I tested to power off the AP, so the app must have been disconnected from the AP, my app still return the SSID of the AP when I use WifiManager.getConnectionInfo().getSSID() to check the SSID, why? And how can I update the current SSID when it is not connected, even it return "unknown ssid"?
Below is the method I check the SSID, the app will invoke this method continuously:
WifiManager wifiManager; // <- In MainAativity
// ....
int checkWiFiSSID() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
Log.d(TAG, "WiFi is disabled, enable it now");
wifiManager.setWifiEnabled(true);
} else {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
// This line always show the AP SSID even the AP is powered down, why??
Log.d(TAG, "AP SSID: " + ssid);
}
}
Thank you.
UPDATE:
Having read the document carefully, I found that if the WifiManager is trying to connect to an AP, even it is not connected, the getSSID() will return its SSID. As my app is keep trying to connect to the AP once it is disconnected. I guess this is the reason, but I am not sure.
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