0

I am building my code for Android Nougat.

I have given location and WiFi permission in manifest file, still, I am getting "0" from wlan_Manager.getScanResults();

my code snippet is

wlan_Manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wlan_switch = (Switch)findViewById(R.id.switch1);
    list = (ListView)findViewById(R.id.listview);

wlan_switch.setChecked(wlan_Manager.isWifiEnabled());

wifi_list = new ArrayList<>();

adapter = new Adapter(this,R.layout.default_listview,wifi_list);
list.setAdapter(adapter);
wifireceiver = new wifi_Receiver(adapter,wifi_list);

registerReceiver(wifireceiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

.

public void onClick(View view) {
    Log.d("switch", "onClick: ");
    if (wlan_switch.isChecked() && !wlan_Manager.isWifiEnabled())
    {
        Log.d(TAG, "true: ");
        wlan_Manager.setWifiEnabled(true);
        wifi_list.clear();

        wlan_Manager.startScan();
    }
    else {
        Log.d(TAG, "false ");
        wlan_Manager.setWifiEnabled(false);
        wifi_list.clear();
        adapter.clear();
    }
}

.

 @Override
    public void onReceive(Context context, Intent intent)
    {
            int size =0 ;
            //scan_Result.clear();
            Log.d("WifScanner", "onReceive");
            wlan_Manager = (WifiManager)context.getApplicationContext().getSystemService(context.WIFI_SERVICE);
            scan_Result = wlan_Manager.getScanResults();
            size = scan_Result.size();

            Log.d(TAG, "onReceive: " + size);

            while (size > 0)
            {
                Log.d(TAG, "size : "+size);
                size--;
                wifi_list.add(scan_Result.get(size).SSID);
                adapter.notifyDataSetChanged();
            }
    }

But when I build code for the Lower android version it is showing WiFi list.

abhishek
  • 11
  • 3

2 Answers2

1

It's one problem in version of Android 7.x. Exists two solutions :

  1. Update version your device for 8.
  2. Edit build.gradle for targetSdkVersion 22
  • I had all of the permissions in place but for me wifiManager.startScan() wasn't doing anything and getScanResults() was returning 0 until I changed targetSdkVersion to 22 for Android Pie(9) – Nicolae Stroncea Sep 08 '19 at 03:04
0

You are calling getScanResults() before you started any scan. For your use case you need to start wifi scanning with the startScan()-method and register a BroadcastReceiver which filter for intents with the action:

WifiManager.SCAN_RESULTS_AVAILABLE_ACTION

Here you can find some example code: Android Wifi Scan - BroadcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION not getting called

Piwo
  • 1,347
  • 1
  • 12
  • 19
  • I have used the same. – abhishek Aug 22 '17 at 08:16
  • @Override public void onReceive(Context context, Intent intent) { int size =0 ; wlan_Manager = (WifiManager)context.getApplicationContext().getSystemService(context.WIFI_SERVICE); scan_Result = wlan_Manager.getScanResults(); size = scan_Result.size(); Log.d(TAG, "onReceive: " + size); while (size > 0) { size--; wifi_list.add(scan_Result.get(size).SSID); adapter.notifyDataSetChanged(); } } – abhishek Aug 22 '17 at 08:21