Solution to the problem of Android10 automatically connecting WiFi

  • 2021-11-30 01:30:36
  • OfStack

Description:

This paper mainly explains the processing of automatically connecting WiFi after scanning code. The process of scanning code is relatively simple, and there are many online tutorials. There is not much change in each version of Android at present.

Problem description:

Recently, when I was doing the project, I found that the previous project had the function of scanning 2D code and automatically connecting WiFi. The device changed the way of generating 2D code, and then I found that the mobile phone could not automatically connect WiFi.

Cause of problem:

After code debugging, I found: (I am all real machine debugging)


wifiManager.addNetwork(WifiConfiguration);

When adding WiFi, this line of code always returns-1, and it can be magically connected by changing to a colleague's mobile phone, and then one face is blinded and cracked, so I am not afraid of problems, but I am afraid that some have problems and some have no problems.

Problem solving:

Difference: I tested the system of mobile phone Xiaomi 10 android Q (andorid 10), and my colleague's mobile phone glorified the system of android P, and boldly guessed if android 10 did something strange again

Root cause: God pays off, code:


/**
   * Add a new network description to the set of configured networks.
   * The {@code networkId} field of the supplied configuration object
   * is ignored.
   * <p/>
   * The new network will be marked DISABLED by default. To enable it,
   * called {@link #enableNetwork}.
   *
   * @param config the set of variables that describe the configuration,
   *      contained in a {@link WifiConfiguration} object.
   *      If the {@link WifiConfiguration} has an Http Proxy set
   *      the calling app must be System, or be provisioned as the Profile or Device Owner.
   * @return the ID of the newly created network description. This is used in
   *     other operations to specified the network to be acted upon.
   *     Returns {@code -1} on failure.
   *
   * @deprecated
   * a) See {@link WifiNetworkSpecifier.Builder#build()} for new
   * mechanism to trigger connection to a Wi-Fi network.
   * b) See {@link #addNetworkSuggestions(List)},
   * {@link #removeNetworkSuggestions(List)} for new API to add Wi-Fi networks for consideration
   * when auto-connecting to wifi.
   * <b>Compatibility Note:</b> For applications targeting
   * {@link android.os.Build.VERSION_CODES#Q} or above, this API will always return {@code -1}.
   */
  @Deprecated
  public int addNetwork(WifiConfiguration config) {
    if (config == null) {
      return -1;
    }
    config.networkId = -1;
    return addOrUpdateNetwork(config);
  }

This is the description of the addNetwork method in WifiManager. class, notice the last line in the comment

{@link android.os.Build.VERSION_CODES#Q} or above, this API will always return {@code -1}.

android Q or later, this method always returns-1, so the cause of the problem is analyzed, and then we will start to solve it: official website 1 operation: the new solution of Android 10 is as follows: https://developer.android.google.cn/guide/topics/connectivity/wifi-bootstrap

The code is as follows:


public void test()
  {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q)
    {
      NetworkSpecifier specifier =
          new WifiNetworkSpecifier.Builder()
              .setSsidPattern(new PatternMatcher(" Here WiFi Name ", PatternMatcher.PATTERN_PREFIX))
              .setWpa2Passphrase(" Here WiFi Password ")
              .build();
 
      NetworkRequest request =
          new NetworkRequest.Builder()
              .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
              .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
              .setNetworkSpecifier(specifier)
              .build();
 
      ConnectivityManager connectivityManager = (ConnectivityManager)
          context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
      ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
          // do success processing here..
        }
 
        @Override
        public void onUnavailable() {
          // do failure processing here..
        }
      };
      connectivityManager.requestNetwork(request, networkCallback);
      // Release the request when done.
      // connectivityManager.unregisterNetworkCallback(networkCallback);
    }
  }

Note: I use the encryption mode of WPA, which is available for pro-test. This is the end, sprinkle flowers.


Related articles: