title: Android connect to Wifi programmatically if wifi name match
body: <p>I would like to build an app, that checks all the available WiFi networks,</p>
  
  <p>If a network's SSID matches a search key then connect to that network, if two networks match then connect to the one with the higher signal strength.</p>
  
  <p>e.g. SearchKey = "Open";</p>
  
  <p>here is the code to check check all the wifi names :</p>
  
  <pre><code>    if (networkInfo.isConnected()) {
                  ArrayList&lt;ScanResult&gt; mItems = new ArrayList&lt;ScanResult&gt;();
              List&lt;ScanResult&gt; results = wifiManager.getScanResults();
              int size  = results.size();
              HashMap&lt;String, Integer&gt; signalStrength = new HashMap&lt;String, Integer&gt;();
              try {
                  for (int i = 0; i &lt; size; i++) {
                      ScanResult result = results.get(i);
                      if (!result.SSID.isEmpty()) {
                          String key = result.SSID + " " + result.capabilities;
                          Log.i("TAG", "ssid: " + result.SSID + " | level: " + result.level);
  }
  </code></pre>
  
  <p>Then I would need to </p>
  
  <pre><code>Arrays.asList(mItems).contains("Open")
  </code></pre>
  
  <p>I am stuck here, How to do a proper check if the keyword "Open" exits, if so, then get the whole name and use below. ?</p>
  
  <p>online sample how to conenct. </p>
  
  <pre><code>WifiConfiguration wifiConfig = new WifiConfiguration();
  wifiConfig.SSID = String.format("\"%s\"", ssid);
  wifiConfig.preSharedKey = String.format("\"%s\"", key);
  
  WifiManager wifiManager = (WifiManager).getSystemService(WIFI_SERVICE);
  //remember id
  int netId = wifiManager.addNetwork(wifiConfig);
  wifiManager.disconnect();
  wifiManager.enableNetwork(netId, true);
  wifiManager.reconnect();
  </code></pre>
  
  <p>UPDATE CODE ------------------------------  - --------------------------------- - -- ---------------------------------------</p>
  
  <p>How to check, This works But Now I need to implement Search by signal strength </p>
  
  <pre><code>if (!result.SSID.isEmpty()) {
                          String key = result.SSID + " " + result.capabilities;
                          Log.i("TAG", "ssid: " + result.SSID + " | level: " + result.level);
  
  
                          if(result.SSID.contains("Open")) {
                              String useSSID = result.SSID;
                              Log.w(TAG, "useSSID =&gt; " + useSSID);
  
                              connectToWifi(MainActivity.this, useSSID);
                              break;
                          }
                          else { Log.e(TAG, "NO result  contains"); }
  </code></pre>
  
  <p>Now how can I query my :</p>
  
  <pre><code>ArrayList&lt;ScanResult&gt; mItems = new ArrayList&lt;ScanResult&gt;();
  
  
                  if (!signalStrength.containsKey(key)) {
                      signalStrength.put(key, i);
                      mItems.add(result);
                  } else {
                      int position = signalStrength.get(key);
                      ScanResult updateItem = mItems.get(position);
                      if (calculateSignalStength(wifiManager, updateItem.level) &gt; calculateSignalStength(wifiManager, result.level)) {
                          mItems.set(position, updateItem);
                      }
                  }
  
  
                       if(mItems.contains("Open")) {
                                  String useSSID = mItems #how to the name SSID name from mItems ???
                                  Log.w(TAG, "useSSID =&gt; " + useSSID);
  
                                  connectToWifi(MainActivity.this, useSSID);
                                  break;
                              }
                              else { Log.e(TAG, "NO result  contains"); }
  </code></pre>
  
  <p>=== <strong>Now my question is how to the SSID name from mItems ?</strong>
  Thanks guys for your help.</p>