title: Force Android app to connect to a certain WiFi network
body: <p><strong>I'm building an app that connects to a certain WiFi network that has no internet access,</strong> I added the network configuration through the app (not through the settings) so I can have the ability to enable/disable this network.</p>
  
  <p>The application works as follows:</p>
  
  <p>1-The user presses a button.</p>
  
  <p>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).</p>
  
  <p>3-I connect to the network using the following methods(After enabling WiFi) :  </p>
  
  <pre><code>mWifiManager.enableNetwork(networkId, true);//I have the network ID from the list of configured networks.
  mWifiManager.reconnect();
  </code></pre>
  
  <p>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.</p>
  
  <p>I tried giving a high priority to my network(Higher than all other configured networks), but it didn't make any difference.</p>
  
  <p>I tried also enabling the network before enabling the WiFi but it didn't work.</p>
  
  <p>I tried adding a mWifiManager.disconnect(); but no change.</p>
  
  <pre><code>mWifiManager.disonnect();
  mWifiManager.enableNetwork(networkId, true);
  mWifiManager.reconnect();
  </code></pre>
  
  <p>So I have some questions :</p>
  
  <p><em>Q1-Why does it work when you are already connected to a WiFi network, but not if WiFi is disabled?</em></p>
  
  <p><em>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).</em></p>
  
  <p>Here is my code: </p>
  
  <pre><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&lt;WifiConfiguration&gt; 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&lt;WifiConfiguration&gt; configuredNetworks = mWifiManager.getConfiguredNetworks();
      if (configuredNetworks != null) {
          for (WifiConfiguration existingConfig : configuredNetworks) {
              if (config.priority &lt;= existingConfig.priority) {
                  config.priority = existingConfig.priority + 1;
              }
          }
      }
  }
  </code></pre>