Android10自動連接WiFi問題的解決
說明:
本文主要說明掃碼之后自動連接WiFi的一些處理,掃碼的流程相對簡單,網上教程也比較多,對于目前Android各個版本也沒有太多變化。
問題描述:
最近在做項目的時候,發(fā)現以前的項目有掃描二維碼自動連接WiFi的功能,設備改了生成二維碼的方式,然后發(fā)現手機無法自動連接WiFi了。
問題原因:
經過代碼調試發(fā)現:(我都是真機調試)
wifiManager.addNetwork(WifiConfiguration);
在添加WiFi的時候,這行代碼始終返回-1,換用同事手機竟然神奇的可以連接,然后一臉蒙蔽,裂開了,不怕有問題,就怕有的有問題,有的沒問題。
問題解決:
區(qū)別:我測試手機 小米10 android Q(andorid 10)的系統(tǒng),同事手機榮耀 android P的系統(tǒng),大膽猜測是不是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() {@Overridepublic void onAvailable(Network network) { // do success processing here..} @Overridepublic void onUnavailable() { // do failure processing here..} }; connectivityManager.requestNetwork(request, networkCallback); // Release the request when done. // connectivityManager.unregisterNetworkCallback(networkCallback); } }
注:我用的是WPA的 加密模式,親測可用。至此完結,撒花。
到此這篇關于Android10自動連接WiFi問題的解決的文章就介紹到這了,更多相關Android10自動連接WiFi內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
