這篇文章主要介紹Android10如何解決自動(dòng)連接WiFi問(wèn)題,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷,提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、網(wǎng)站開(kāi)發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營(yíng)銷、成都微信小程序、公眾號(hào)商城、等建站開(kāi)發(fā),創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。
說(shuō)明:
本文主要說(shuō)明掃碼之后自動(dòng)連接WiFi的一些處理,掃碼的流程相對(duì)簡(jiǎn)單,網(wǎng)上教程也比較多,對(duì)于目前Android各個(gè)版本也沒(méi)有太多變化。
問(wèn)題描述:
最近在做項(xiàng)目的時(shí)候,發(fā)現(xiàn)以前的項(xiàng)目有掃描二維碼自動(dòng)連接WiFi的功能,設(shè)備改了生成二維碼的方式,然后發(fā)現(xiàn)手機(jī)無(wú)法自動(dòng)連接WiFi了。
問(wèn)題原因:
經(jīng)過(guò)代碼調(diào)試發(fā)現(xiàn):(我都是真機(jī)調(diào)試)
wifiManager.addNetwork(WifiConfiguration);
在添加WiFi的時(shí)候,這行代碼始終返回-1,換用同事手機(jī)竟然神奇的可以連接,然后一臉蒙蔽,裂開(kāi)了,不怕有問(wèn)題,就怕有的有問(wèn)題,有的沒(méi)問(wèn)題。
問(wèn)題解決:
區(qū)別:我測(cè)試手機(jī) 小米10 android Q(andorid 10)的系統(tǒng),同事手機(jī)榮耀 android P的系統(tǒng),大膽猜測(cè)是不是android 10又搞了什么奇怪的東西
根因:皇天不負(fù)有心人,上代碼:
/** * 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或者更高的版本,這個(gè)方法始終返回-1,至此問(wèn)題原因分析完畢,接下來(lái)開(kāi)始解決:官網(wǎng)一頓操作: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的 加密模式,親測(cè)可用。至此完結(jié),撒花。
以上是“Android10如何解決自動(dòng)連接WiFi問(wèn)題”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前題目:Android10如何解決自動(dòng)連接WiFi問(wèn)題
當(dāng)前路徑:http://aaarwkj.com/article40/gihdho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、云服務(wù)器、Google、品牌網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)