title: Connect to specific WiFi (Android Q)
body: <p>In my app, I'm trying to connect automatically to a specific SSID (in android Q). I'm using &quot;WifiNetworkSpecifier&quot;,&quot;NetworkRequest&quot; and &quot;ConnectivityManager&quot; classes to do that. When I do a request, then a dialog appears on screen asking me to connect to this SSID, I click and then it connects. But there are two problems:</p>
  <ol>
  <li><p>It's not really connected (no internet), I have to disable and enable WiFi to have the chance to be really connected!</p>
  </li>
  <li><p>When I clear app from memory it is automatically disconnected.</p>
  </li>
  </ol>
  <p>I'm looking to resolve these problems, but there is not enough example or sample.
  This is what I do in my source code:</p>
  <pre><code>fun connectToWifi(networkSSID: String, networkPassword: String?, bssid: String?)
          val specifier = WifiNetworkSpecifier.Builder()
              .setSsid(networkSSID)
          if (networkPassword != null) {
              specifier.setWpa2Passphrase(networkPassword)
          }
          if (bssid != null) {
              specifier.setBssid(MacAddress.fromString(bssid))
          }
          val request = NetworkRequest.Builder()
              .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
              .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
              .setNetworkSpecifier(specifier.build())
              .build()
          val connectivityManager =
              singletonArgument.context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
          val networkCallback = object : ConnectivityManager.NetworkCallback() {
              override fun onAvailable(network: Network?) {
                  Log.d(TAG, &quot;network available&quot;)
              }
  
              override fun onUnavailable() {
                  Log.d(TAG, &quot;network unavailable&quot;)
              }
          }
          connectivityManager.requestNetwork(request, networkCallback)
  }
  </code></pre>