溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android10如何解決自動連接WiFi問題

發(fā)布時間:2021-05-07 14:25:56 來源:億速云 閱讀:634 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹Android10如何解決自動連接WiFi問題,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

說明:

本文主要說明掃碼之后自動連接WiFi的一些處理,掃碼的流程相對簡單,網(wǎng)上教程也比較多,對于目前Android各個版本也沒有太多變化。

問題描述:

最近在做項目的時候,發(fā)現(xiàn)以前的項目有掃描二維碼自動連接WiFi的功能,設(shè)備改了生成二維碼的方式,然后發(fā)現(xiàn)手機(jī)無法自動連接WiFi了。

問題原因:

經(jīng)過代碼調(diào)試發(fā)現(xiàn):(我都是真機(jī)調(diào)試)

wifiManager.addNetwork(WifiConfiguration);

在添加WiFi的時候,這行代碼始終返回-1,換用同事手機(jī)竟然神奇的可以連接,然后一臉蒙蔽,裂開了,不怕有問題,就怕有的有問題,有的沒問題。

問題解決:

區(qū)別:我測試手機(jī) 小米10 android Q(andorid 10)的系統(tǒng),同事手機(jī)榮耀 android P的系統(tǒng),大膽猜測是不是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或者更高的版本,這個方法始終返回-1,至此問題原因分析完畢,接下來開始解決:官網(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的 加密模式,親測可用。至此完結(jié),撒花。

以上是“Android10如何解決自動連接WiFi問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI