2

With Android 10, I'm using the following method to connect to my Wifi Access Point :

@RequiresApi(api = Build.VERSION_CODES.Q)
public static void connectToWifiAccessPoint(String AP_SSID, String AP_PASSWORD, ConnectivityManager connectivityManager) {

    WifiNetworkSpecifier.Builder builder = null;
    builder = new WifiNetworkSpecifier.Builder();

    builder.setSsid(AP_SSID);
    builder.setWpa2Passphrase(AP_PASSWORD);

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
    NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
    networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
    NetworkRequest networkRequest = networkRequestBuilder.build();

    connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                connectivityManager.bindProcessToNetwork(network);
            }
    });
}

A few seconds after calling this method, the OS displays this pop-up:

Device to use with app

After hitting the connect button, the same pop-up appears a few seconds later, and the smartphone is still not connected to the access point.

Any idea how to fix this please ?

matdev
  • 4,115
  • 6
  • 35
  • 56

2 Answers2

1

I struggle couple days ago with wifi connection on Andrdoid Q, the link of my question in stackOverflow.

So, the solution is compile your app with targetSdkVersion 28. and for connection to wifi use this function :

 public void connectToWifi(String ssid, String key) {

    Log.e(TAG, "connection wifi pre Q");
    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = "\"" + ssid + "\"";
    wifiConfig.preSharedKey = "\"" + key + "\"";
    int netId = wifiManager.addNetwork(wifiConfig);
    if (netId == -1) netId = getExistingNetworkId(wifiConfig.SSID);

    wifiManager.disconnect();
    wifiManager.enableNetwork(netId, true);
    wifiManager.reconnect();
}
NizarETH
  • 969
  • 3
  • 15
  • 38
  • 1
    Thanks, but this solution will be invalid soon: from November 2, 2020, a targetSdkVersion of at least 29 is required for app updates. – matdev Oct 26 '20 at 13:22
  • 1
    Yes I know, but Google developers should find a solution for that, this is just a workaround for the moment – NizarETH Oct 26 '20 at 16:15
  • Yes indeed. Here is the issue reported to Google : https://issuetracker.google.com/issues/138335744 – matdev Oct 26 '20 at 16:53
0

Try to forget the wifi Access point from saved networks

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 12 '22 at 23:07