unable to implement auto wifi scanning

37 Views Asked by At

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

0

There are 0 best solutions below