title: Android Studio: getScanResults returns empty but permission is given
body: <p>I am trying to create a code that returns me available Wi-Fi networks. But I am facing a problem. getScanResults returns empty, though I have given all possible network permissions.</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private static final String  TAG = "My Activity";
      private StringBuilder sb = new StringBuilder();
      private TextView tv;
      List&lt;ScanResult&gt; scanList;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          final String CoarseLocation = Manifest.permission.ACCESS_COARSE_LOCATION;
          final String AccessWifi = Manifest.permission.ACCESS_WIFI_STATE;
          final String ChangeWifi = Manifest.permission.CHANGE_WIFI_STATE;
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          tv = (TextView) findViewById(R.id.textView2);
          Button btn = (Button) findViewById(R. id. button);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  if (checkSelfPermission(CoarseLocation) != PackageManager.PERMISSION_GRANTED)
                  {
                      requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 123);
                  }
  
                  if (checkSelfPermission(AccessWifi) != PackageManager.PERMISSION_GRANTED)
                  {
                      requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE}, 123);
                  }
  
                  if (checkSelfPermission(ChangeWifi) != PackageManager.PERMISSION_GRANTED)
                  {
                      requestPermissions(new String[]{Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE}, 123);
                  }
  
                  LocationManager lman = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                  boolean network_enabled = false;
  
                  try
                  {
                      network_enabled = lman.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                  } catch (Exception ex) {}
  
                  if (!network_enabled)
                  {
                      startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
                  }
  
                  final WifiManager mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
                  mWifiManager.setWifiEnabled(true);
  
                  IntentFilter filter = new IntentFilter();
                  filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  
                  registerReceiver(new BroadcastReceiver() {
                      @Override
                      public void onReceive(Context context, Intent intent) {
  
                          List&lt;ScanResult&gt; results = mWifiManager.getScanResults();
                          final int Amount = results.size();
  
                          Log.v(TAG, "Wifi Scan Results Count: " + Amount);
  
                          int num = 1;
  
                          while (num &lt;= Amount)
                          {
                              Log.v(TAG, "SSID  =  " + results.get(num).SSID);
  
                              num = num+1;
                          }
                      }
                  }, filter);
  
                  mWifiManager.startScan();
              }
          });
      }
  }
  </code></pre>
  
  <p>Update:</p>
  
  <p>Here is the error that I get when I try to scan Wi-Fi when location services are enabled. I am into android programming only for a month so can anyone explain what that error means?</p>
  
  <pre><code>10-15 21:55:21.843 21441-21441/com.example.gytis.imlost E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.gytis.imlost, PID: 21441
        java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 (has extras) } in com.example.gytis.imlost.MainActivity$1$1@a300383
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:935)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5527)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
         Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at com.example.gytis.imlost.MainActivity$1$1.onReceive(MainActivity.java:110)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:925)
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.app.ActivityThread.main(ActivityThread.java:5527) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
  </code></pre>
  
  <p>Please help. Thanks!</p>