I'm building an app that connects to a certain WiFi network that has no internet access, I added the network configuration through the app (not through the settings) so I can have the ability to enable/disable this network.
The application works as follows:
1-The user presses a button.
2-I check if WiFi is enabled, if not, I enable it. Also, if the phone is already connected to a WiFi network, I disconnect it from that network and connect to mine(Which works fine).
3-I connect to the network using the following methods(After enabling WiFi) :
mWifiManager.enableNetwork(networkId, true);//I have the network ID from the list of configured networks.
mWifiManager.reconnect();
But this doesn't seem to work if the WiFi was initially disabled, WiFi gets enabled but doesn't always connect to my network, it sometimes connects to other configured networks if available.
I tried giving a high priority to my network(Higher than all other configured networks), but it didn't make any difference.
I tried also enabling the network before enabling the WiFi but it didn't work.
I tried adding a mWifiManager.disconnect(); but no change.
mWifiManager.disonnect();
mWifiManager.enableNetwork(networkId, true);
mWifiManager.reconnect();
So I have some questions :
Q1-Why does it work when you are already connected to a WiFi network, but not if WiFi is disabled?
Q2-Is there any working way to force an app to connect to a specific network?(I'm working on Android 7.0 API 24).
Here is my code:
int mID;
String mSSID;
String mPASSWORD;
public void connectToWiFi(String ssid, String key) {
mSSID = ssid;
mPASSWORD = key;
if (!mWifiManager.isWifiEnabled()) {
WifiUtils.withContext(mContext).enableWifi(this::enableResult);
//Also tried
// boolean wifiEnabled = mWifiManager.setWifiEnabled(true);
return;
}
connectToNetwork();
}
private void enableResult(boolean b) { //This is to make sure WiFi has been enabled successfully
Log.w(TAG, "enableResult:" + b);
if (b) {
connectToNetwork();
}
}
private void connectToNetwork() {
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);// also tried to store the id in shared prefs and get it back here (did not work for me)
mID = getExistingNetworkId(mSSID);
if (mID == -1) {//Network has not been configured yet, so I add it
mID = addNetworkToConfiguration();
MyApplication.getInstance().getShared().setNetowrkID(mID);// store the id in shred prefs
mWifiManager.saveConfiguration();
}
boolean disconnect = mWifiManager.disconnect();
boolean result = mWifiManager.enableNetwork(mID, true);
boolean reconnect = mWifiManager.reconnect();
}
private int addNetworkToConfiguration() {
WifiConfiguration config = new WifiConfiguration();
config.SSID = quoted(mSSID);
config.status = WifiConfiguration.Status.ENABLED;
config.preSharedKey = quoted(mPASSWORD);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
assignHighestPriority(config);
return mWifiManager.addNetwork(config);
}
private int getExistingNetworkId(String SSID) {
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredNetworks = mWifiManager.getConfiguredNetworks();
if (configuredNetworks != null) {
for (WifiConfiguration existingConfig : configuredNetworks) {
if (areEqual(trimQuotes(existingConfig.SSID), trimQuotes(SSID))) {
return existingConfig.networkId;
}
}
}else {
Log.e(TAG,"Unable to fetch network configurations");
}
return -1;}
private void assignHighestPriority(WifiConfiguration config) {
List<WifiConfiguration> configuredNetworks = mWifiManager.getConfiguredNetworks();
if (configuredNetworks != null) {
for (WifiConfiguration existingConfig : configuredNetworks) {
if (config.priority <= existingConfig.priority) {
config.priority = existingConfig.priority + 1;
}
}
}
}