1

I am struggling through one issue that is- I am trying to connect with a SSID which is WPA2 PSK by below code:

             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(); 

Above code is working fine upto KitKat. But i am facing some inconsistency issue in Lollipop. In Goggle nexus and Motorola 2nd gen some time its not connected and some its connected successfully but after some time spontaneous it get disconnect. Then after some Google i found some more permission we need to give then i have done this

         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);
              }

But still same issue, Please get me rid of this issue. What i am missing here.

Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42

0 Answers0