illustrate:
This article mainly explains some processing to automatically connect to WiFi after scanning the code. The process of scanning the code is relatively simple, and there are many online tutorials, and there are not many changes to the current version of Android.
Problem description:
Recently, when I was working on a project, I found that my previous project had the function of scanning the QR code to automatically connect to WiFi. The device changed the method of generating the QR code, and then I found that the mobile phone could not automatically connect to WiFi.
Cause of the problem:
After code debugging, I found that: (I'm all real computer debugging)
(WifiConfiguration);
When adding WiFi, this line of code always returns -1. When using a colleague's phone, it can be magically connected. Then it looks blind and cracked. It is not afraid of any problems, but is afraid that some have problems and some are fine.
Problem Solving:
Difference: I tested the system of Xiaomi 10 android Q (andorid 10), my colleague's system of Honor android P, and boldly guessed if android 10 has made something strange
The root cause: God pays off for those who are interested, 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 #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 .VERSION_CODES#Q} or above, this API will always return {@code -1}. */ @Deprecated public int addNetwork(WifiConfiguration config) { if (config == null) { return -1; } = -1; return addOrUpdateNetwork(config); }
This is the description of the addNetwork method, pay attention to the last line in the comment
{@link .VERSION_CODES#Q} or above, this API will always return {@code -1}.
Android Q or higher version, this method always returns -1. After the analysis of the cause of this problem, we will start to solve it: Official website operation: Android 10’s new solution is as follows:/guide/topics/connectivity/wifi-bootstrap
The code is as follows:
public void test() { if (.SDK_INT >= .VERSION_CODES.Q) { NetworkSpecifier specifier = new () .setSsidPattern(new PatternMatcher("The name of WiFi here", PatternMatcher.PATTERN_PREFIX)) .setWpa2Passphrase("WiFi Password Here") .build(); NetworkRequest request = new () .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .setNetworkSpecifier(specifier) .build(); ConnectivityManager connectivityManager = (ConnectivityManager) (Context.CONNECTIVITY_SERVICE); networkCallback = new () { @Override public void onAvailable(Network network) { // do success processing here.. } @Override public void onUnavailable() { // do failure processing here.. } }; (request, networkCallback); // Release the request when done. // (networkCallback); } }
Note: I use WPA encryption mode, which can be used for personal testing. It's over here and sprinkle flowers.
This is the article about solving the problem of automatically connecting to WiFi on Android 10. For more information about Android 10, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!