0

I am developing a wifi manager app for android. here's the code that i am using to scan the networks

public void scanWifiList() {
       
            // Unregister the previous instance of the BroadcastReceiver
            try {

                unregisterReceiver(wifiReceiver);
            }catch (Exception e){}
            registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

            scanninghandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    wifiManager.startScan();

                     // Schedule the next scan after 5 seconds
                    scanninghandler.postDelayed(runnable, 5000);
                }
            }, 1000); 
        
    }

    class WifiScanReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            if (ActivityCompat.checkSelfPermission(c, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

            if(wifiScanList != null)
                wifiScanList.clear();
            wifiScanList = wifiManager.getScanResults();

            
            wifiAdapter = new WifiAdapter(WifiNetworkList.this, sortedwifilist(wifiScanList),connected_ssid);
all_wifi.setAdapter(wifiAdapter);
            unregisterReceiver(this);

        }

as you can see I am using handler to repeat scanning again and again after 5sec to get updated available networks but the issue here is that I'm only able to scan new wifi for the first 2,3 times and than it just keeps returning the old wifilist.

i want to update the wifilist after every 5sec so that the user can see all the new networks available, Thanks

  • Please read https://developer.android.com/guide/topics/connectivity/wifi-scan Since Android 9 foreground apps can perform a Wifi scan every 2 minutes. In background every 30 minutes. – Robert May 17 '23 at 09:43
  • @Robert thanks for sharing this reference, I am creating this auto scanning mechanism in my activity class so this means that foreground policy will be applied in my case and i can get the updated wifi networks every 2mins, is this right? – Qasim Malik May 18 '23 at 07:30

0 Answers0