0

I Want to get a List of available WIFis and I followed this question: how to get available wifi networks and display them in a list in android

But there is a problem that most of the time it doesn't return WiFi list! (When I run my application on devices below android 6 it works well.)

This is my code:

  WifiManager mainWifi;
  WifiReceiver receiverWifi;
  List<ScanResult> wifiList;
  StringBuilder sb = new StringBuilder();

  private final Handler handler = new Handler();

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new PermissionHandler().checkPermission(this, permissions, new PermissionHandler.OnPermissionResponse() {
      @Override
      public void onPermissionGranted() {

        mainWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (mainWifi.isWifiEnabled() == false)
        {
          // If wifi disabled then enable it
          Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
            Toast.LENGTH_LONG).show();

          mainWifi.setWifiEnabled(true);
        }
        receiverWifi = new WifiReceiver();
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        mainWifi.startScan();
      }

      @Override
      public void onPermissionDenied(Activity activity, String[] deniedPermissions, PermissionHandler.OnPermissionResponse listener) {

      }
    });
  }

  class WifiReceiver extends BroadcastReceiver {

    // This method call when number of wifi connections changed
    public void onReceive(Context c, Intent intent) {

      sb = new StringBuilder();
      wifiList = mainWifi.getScanResults();
      sb.append("\n        Number Of Wifi connections :"+wifiList.size()+"\n\n");

      for(int i = 0; i < wifiList.size(); i++){

        sb.append(new Integer(i+1).toString() + ". ");
        sb.append((wifiList.get(i)).toString());
        sb.append("\n\n");

        Log.i("LOGO_WIFI", sb.toString());
      }

    }
  }

And I got these permissions in the manifest and at runtime:

Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
Manifest.permission.ACCESS_WIFI_STATE
Manifest.permission.CHANGE_WIFI_STATE
Manifest.permission.ACCESS_NETWORK_STATE
Manifest.permission.CHANGE_NETWORK_STATE
Ehsan
  • 2,676
  • 6
  • 29
  • 56

3 Answers3

0

mainWifi.getScanResults() returns you a List of ScanResults that you after parse to get all the information that you want.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Tomás Rodrigues
  • 519
  • 2
  • 17
0

I Just find out the problem with my code.

The problem was about GPS! I got Location permissions from the user but the GPS was off and because of that I didn't get any list till now!

Now when the GPS is on I can get available Wifi list and when GPS is off I'll get nothing!

Is there any way to get this list without turning on GPS?

Ehsan
  • 2,676
  • 6
  • 29
  • 56
0

mainWifi.getScanResults() will return 0 results if you don't have runtime permission (in Android 6.0+ devices). Add runtime permission and you will be good to go.

Handle Runtime Permission after Android 6.0

With the release of Android 6.0, the way permissions are granted changed in an App changed. If you are planning to run your app in Android 6.0+ devices then you need to have runtime permission as well along with the permissions declared in the Manifest.

How to add runtime permission in the code?

Let's say you want to access android.permission.ACCESS_COARSE_LOCATION then,

  1. Add it in the Manifest file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  1. Add this in the code
@Override
public void onResume()
{
    super.onResume();

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 87);
        }
    }
}

More points:

1) You don't need to have both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION. ACCESS_COARSE_LOCATION is enough

2) You will still get 0 results if the GPS is turned off.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88