0

I am using WifiManager to get a list of available wifi networks.

This is my method:

 public List<ScanResult> getWifiInRange() {

    //scan for wifis
    wifiMgr.startScan();

    // gets ~last~ list of WiFi networks accessible through the access point.
    return (wifiScan = (List<ScanResult>) wifiMgr.getScanResults());
  }

But this method doesn't retun any wifi networks on some devices!

For example, when I using this method on Asus_p024 it works correctly and returns a list of available wifi networks But on Samsung Tab-s it returns 0 items!

Update

I just changed my code and implement a broadcast like this:

public class WifiBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

      List<ScanResult> mScanResults = wifi.getScanResults();

      wifiList = wifi.getScanResults();
      netCount = wifiList.size();

      Log.d("Wifi", "Total Wifi Network" + netCount);

    }
  }

I still it can't get a list of available wifi networks.

Thank you for your help.

Ehsan
  • 2,676
  • 6
  • 29
  • 56

2 Answers2

1

Try registering a BroadcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION as described here. The docs for WifiManager.getScanResults() say:

Return the results of the latest access point scan.

So we don't have certainty that the result of the latest scan is available at the call to getScanResults()

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
1

Pleae check that you have included the following permissions in your manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Additionally, for API 23+, permissions must be requested at runtime.

Yuliwee
  • 327
  • 1
  • 11