1

我正在努力解决一个问题 - 我正在尝试通过以下代码连接 WPA2 PSK 的 SSID:

             WifiConfiguration conf = new WifiConfiguration();

             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                   conf.SSID =bed_ssid_name;  
          } else {
             conf.SSID = "\"" + bed_ssid_name + "\"";  
          }
             conf.preSharedKey = "\""+ networkPass +"\"";
             wifi.addNetwork(conf);

             List<WifiConfiguration>    list = null;

             list = wifi.getConfiguredNetworks();   
             //wifi.disconnect(); 

             //wifi.disconnect();
             for( WifiConfiguration i : list ) {
                   if(i.SSID != null && i.SSID.equals("\"" + bed_ssid_name + "\"")) {
                          System.out.println("!!!!!!!### several ssid  "+i.SSID +"     ssid     "+"\""+bed_ssid_name+"\"");

                          wifi.enableNetwork(i.networkId, true);
                          break;
                   }                
             }          
             wifi.reconnect(); 

上面的代码在 KitKat 上运行良好。但是我在 Lollipop 中遇到了一些不一致的问题。在 Goggle nexus 和 Motorola 2nd gen 中,有时它未连接,有些连接成功,但在一段时间后自发断开连接。然后经过一些谷歌后,我发现我们需要给予更多的许可,然后我已经完成了

         public void addWifiConfig(String ssid,String password) {
                //  Log.d(TAG, "Inside addWifiConfig...");
                  if (ssid == null) {
                      throw new IllegalArgumentException(
                              "Required parameters can not be NULL #");
                  }

                  String wifiName = ssid;
                  WifiConfiguration conf = new WifiConfiguration();
                  // On devices with version Kitkat and below, We need to send SSID name
                  // with double quotes. On devices with version Lollipop, We need to send
                  // SSID name without double quotes
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                      conf.SSID = wifiName;
                  } else {
                      conf.SSID = Constants.BACKSLASH + wifiName + Constants.BACKSLASH;
                  }
               conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//           conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

               conf.status = WifiConfiguration.Status.ENABLED;        
              // conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
               conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
               conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
               //conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
               conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
               conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                  int newNetworkId = wifi.addNetwork(conf);
                  wifi.enableNetwork(newNetworkId, true);
                  wifi.saveConfiguration();
                  wifi.setWifiEnabled(true);
              }

但仍然是同样的问题,请让我摆脱这个问题。我在这里缺少什么。

4

0 回答 0