Android10自动连接WiFi问题的解决
说明:
本文主要说明扫码之后自动连接WiFi的一些处理,扫码的流程相对简单,网上教程也比较多,对于目前Android各个版本也没有太多变化。
问题描述:
最近在做项目的时候,发现以前的项目有扫描二维码自动连接WiFi的功能,设备改了生成二维码的方式,然后发现手机无法自动连接WiFi了。
问题原因:
经过代码调试发现:(我都是真机调试)
wifiManager.addNetwork(WifiConfiguration);
在添加WiFi的时候,这行代码始终返回-1,换用同事手机竟然神奇的可以连接,然后一脸蒙蔽,裂开了,不怕有问题,就怕有的有问题,有的没问题。
问题解决:
区别:我测试手机 小米10 android Q(andorid 10)的系统,同事手机荣耀 android P的系统,大胆猜测是不是android 10又搞了什么奇怪的东西
根因:皇天不负有心人,上代码:
/** * 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); }
这是WifiManager.class中addNetwork方法的描述,注意注释中最后一行
{@link android.os.Build.VERSION_CODES#Q} or above, this API will always return {@code -1}.
android Q或者更高的版本,这个方法始终返回-1,至此问题原因分析完毕,接下来开始解决:官网一顿操作:Android 10 的新方案如下连接:https://developer.android.google.cn/guide/topics/connectivity/wifi-bootstrap
代码如下:
public void test() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { NetworkSpecifier specifier = new WifiNetworkSpecifier.Builder() .setSsidPattern(new PatternMatcher("此处WiFi名称", PatternMatcher.PATTERN_PREFIX)) .setWpa2Passphrase("此处WiFi密码") .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); } }
注:我用的是WPA的 加密模式,亲测可用。至此完结,撒花。
到此这篇关于Android10自动连接WiFi问题的解决的文章就介绍到这了,更多相关Android10自动连接WiFi内容请搜索海外IDC网以前的文章或继续浏览下面的相关文章希望大家以后多多支持海外IDC网!