title: Android Q, programmatically connect to different WiFi AP for internet
body: <p>As in Android Q, several WiFi APIs are restricted. I am trying to use alternate APIs to connect to different Wifi AP for internet.</p>
  
  <p>Below is my code : </p>
  
  <pre><code>    WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
      builder.setSsid("wifi-ap-ssid");
      builder.setWpa2Passphrase("wifi-ap-password");
  
      WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
  
      NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder();
      networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
      networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);
  
      NetworkRequest nr = networkRequestBuilder1.build();
      ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      cm.requestNetwork(nr, callback);
  </code></pre>
  
  <p>This allows me to connect but Internet is disabled. This is working as defined in Android docs.</p>
  
  <p>Alternate way i tried is below : </p>
  
  <pre><code>    WifiNetworkSuggestion.Builder wifiNetworkSuggestionBuilder1 = new WifiNetworkSuggestion.Builder();
      wifiNetworkSuggestionBuilder1.setSsid("wifi-ap-ssid");
      wifiNetworkSuggestionBuilder1.setWpa2Passphrase("wifi-ap-password");
      WifiNetworkSuggestion wifiNetworkSuggestion = wifiNetworkSuggestionBuilder1.build();
      List&lt;WifiNetworkSuggestion&gt; list = new ArrayList&lt;&gt;();
      list.add(wifiNetworkSuggestion);
      wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      wifiManager.removeNetworkSuggestions(new ArrayList&lt;WifiNetworkSuggestion&gt;());
      wifiManager.addNetworkSuggestions(list);
  </code></pre>
  
  <p>declared permission in Manifest : </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/&gt;
  </code></pre>
  
  <p>Using this didn't change anything in behavior.</p>
  
  <p>Please let know sequence of APIs to connect successfully to different Wifi AP with internet capability.</p>