0

I have a broadcast receiver to scan the available networks, with the normal code that you can easily find here on StackOverflow.

I am putting on my layout an icon for each available network, the problem is that when a network is removed, it still shows it.

The problem is that for this broadcast receiver, the action/intent that it is listening is the SCAN_RESULTS_AVAILABLE_ACTION . In the beginning i can call the startScan(), but after that there will be no more scans. It only scans if i manually go to the wifi settings on my phone.

What would be a possible solution for this ? It should automatically remove the networks that are not available anymore, but in order to do that, it has to scan again, but i don't how i should "trigger" that scan again.

my code is :

  public class WifiReceiver extends BroadcastReceiver {

    private Vector<String> keywords;

    public void onReceive(Context c, Intent intent) {
        System.out.println("TOU AQUI NO WIFI RECEIVER !! ");
        List<ScanResult> connResults=wifimanager.getScanResults();
        List<String> scanResultsSSID = new ArrayList<>() ;

        for(int i=0;i<connResults.size();i++){
            String ssid=connResults.get(i).SSID;

            scanResultsSSID.add(ssid);

            if(ssid.contains("teste")){
                System.out.println("Estou no : " + connResults.get(i).toString());
                radarTextView.addKeyWord(ssid);
            }

        }

        keywords=radarTextView.getKeyWords();

        for(int j=0;j<keywords.size();j++){
            String tmp=keywords.get(j);
            System.out.println("this is one of the keyword : "+ tmp);
            if(!scanResultsSSID.contains(tmp)){
                radarTextView.removeKeyWord(tmp);
                System.out.println("removed this : "+tmp);
            }
        }

        radarTextView.show();


    }



}

I know that i am only adding a network with the name "teste" but that is the name of the wifi on one of my phones that i am testing this. If i turn it off, it doesn't get removed from the other phone.

1 Answers1

1

Use a TimerTask

like this

Timer timer = new Timer();
int DELAY=0;
int INTERVAL=10000;                           

timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            connResults = wifimanager.getScanResults();
        }
    },DELAY,INTERVAL);
shinilms
  • 1,494
  • 3
  • 22
  • 35
  • That is a good option. I am just worried if it will be too consuming for my app, since what i am doing is a lite version (for older devices). I am going to test it and if it is not too consuming, i will definitely accept your answer. Thank you a lot for your help ! – Filipe Gonçalves Sep 01 '16 at 10:21