title: Connect to Wifi in Android Q programmatically
body: <p>I had this function to connect in Wifi network, below Android 10 it works fine, but when I tried on Android 10, I had a successful connection but <strong>WITHOUT internet</strong>, I knew it's <a href="https://issuetracker.google.com/issues/138335744#comment8" rel="noreferrer">a bug in Android 10</a> but I found this <a href="https://play.google.com/store/apps/details?id=com.themausoft.wpsapp&amp;hl=fr" rel="noreferrer">application</a> which can connect to wifi from Android 10 with no problem.
  I'm blocked for days.</p>
  <p>My function :</p>
  <pre><code>private void connectToWifi(String ssid, String password)
      {
          WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
          if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.Q) {
              try {
                  Log.e(TAG,&quot;connection wifi pre Q&quot;);
                  WifiConfiguration wifiConfig = new WifiConfiguration();
                  wifiConfig.SSID = &quot;\&quot;&quot; + ssid + &quot;\&quot;&quot;;
                  wifiConfig.preSharedKey = &quot;\&quot;&quot; + password + &quot;\&quot;&quot;;
                  int netId = wifiManager.addNetwork(wifiConfig);
                  wifiManager.disconnect();
                  wifiManager.enableNetwork(netId, true);
                  wifiManager.reconnect();
  
              } catch ( Exception e) {
                  e.printStackTrace();
              }
          } else {
              Log.e(TAG,&quot;connection wifi  Q&quot;);
  
              WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
                  .setSsid( ssid )
                  .setWpa2Passphrase(password)
                      .build();
  
              NetworkRequest networkRequest = new NetworkRequest.Builder()
                      .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                      .setNetworkSpecifier(wifiNetworkSpecifier)
                      .build();
  
               connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  
  
                   networkCallback = new ConnectivityManager.NetworkCallback() {
                  @Override
                  public void onAvailable(Network network) {
                      super.onAvailable(network);
  
                       connectivityManager.bindProcessToNetwork(network);
                      Log.e(TAG,&quot;onAvailable&quot;);
                  }
  
                    @Override
                    public void onLosing(@NonNull Network network, int maxMsToLive) {
                        super.onLosing(network, maxMsToLive);
                        Log.e(TAG,&quot;onLosing&quot;);
                    }
  
                    @Override
                  public void onLost(Network network) {
                      super.onLost(network);
                      Log.e(TAG, &quot;losing active connection&quot;);
                  }
  
                  @Override
                  public void onUnavailable() {
                      super.onUnavailable();
                      Log.e(TAG,&quot;onUnavailable&quot;);
                  }
              };
              connectivityManager.requestNetwork(networkRequest,networkCallback);
  
  
          }
      }
  </code></pre>