androidandroid-wifiwifi-direct

Android "Bypassing user approval" for the second programmatically connection to a specific Wi-Fi network is not working


Following the Android documentation, I'm using this code to connect programmatically to a specific SSID:

private void connectToAP(String ssid, String password) {
   ...
   WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
     .setSsid(ssid)
     .setWpa2Passphrase(password)
     .build();

   NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder()
     .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
     .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
     .setNetworkSpecifier(wifiNetworkSpecifier);

   NetworkRequest request = requestBuilder.build();
   connManager.requestNetwork(request, myNetworkCallback);
   ...
   bindNetwork(); //this method will bind the network without internet
}

as described in the documentation https://developer.android.com/guide/topics/connectivity/wifi-bootstrap#bypass-approval, if I don't forget the network, the second connection to that specific SSID have to bypass the user approval.

My problem is this:

  1. Call "connectToAP"
  2. User approval
  3. Conneted
  4. After a while the IoT device turns off the wifi for few seconds
  5. The disconnection callback retries to call the "connectToAP" method (Point 1) but the user approval (Point 2) is not bypassed as expected

What I'm doing wrong?


Solution

  • here I attach a example for you val specifier, observe that the magic for this is at put in setBssid a value, with it thats ok and the system don`t show window aproval.

    WifiNetworkSpecifier = if(isDevice8424){
                    WifiNetworkSpecifier.Builder()
                        .setSsid(ssid)
                        .setBssid(MacAddress.fromString(bssid))//here ir the magic!
                        .build()