0

Is there anyway to get the access point details that the phone can detect every second? Currently, I can get access point details but not every second.

I have been tried Thread and BroadcastReceiver, but the access point details will only change after 4-6 seconds. The coding may be mistake.

Thread:

handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while(Running){
                try{
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        number+=1;

                        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
                        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                        List<ScanResult> results = wifiManager.getScanResults();
                        //int count = 1;
                        String WifiListDetails = "";

                        for (ScanResult result : results) {


                            WifiListDetails += number + ")" + result.SSID + "\n";
                        }

                        textViewControlTitle.setText(WifiListDetails);
                    }
                });
            }
        }
    };
    new Thread(runnable).start();

BroadcastReceiver

    private IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
private int number = 0;

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        List<ScanResult> results = wifiManager.getScanResults();

        String WifiListDetails = "";

        for (ScanResult result : results) {

            WifiListDetails += number++ + ")" + result.SSID + "\n";
        }

        Toast.makeText(getBaseContext(),
                WifiListDetails,
                Toast.LENGTH_LONG).show();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


protected void onResume(){
    this.registerReceiver(receiver, filter);
    super.onResume();
}

protected void onPause(){
    this.unregisterReceiver(receiver);
    super.onPause();
}

This question may have been asked by others, but I couldn't find the answer.

Siang
  • 3
  • 4
  • possible duplicate of [How can I get Android Wifi Scan Results into a list?](http://stackoverflow.com/questions/5452940/how-can-i-get-android-wifi-scan-results-into-a-list) – Chaoz May 09 '15 at 16:07
  • @MateiTrandafir but i wish to get the details every seconds and sorry for my question, I have edited it. – Siang May 09 '15 at 16:53

1 Answers1

0

This question is already answered.

This question may have been asked by others, but I couldn't find the answer.

I did 1 Google search and found this.
All you need to do is to get that into a Thread that searches as fast as possible (that may not be every second but every few seconds).

EDIT:

To make that thread, you first need some variables in your main activity:

private Thread thread = null;
private boolean threadRunning = false;

Then you need your actual Thread Runnable object which has the code:

private final Runnable threadRunnable = new Runnable() {
    public void run() {
        while (threadRunning) {
            // your scanning code that needs to be executed every second goes here
            try { sleep(100); } catch(InterruptedException e) {}
            // Don't overclock, give the processor a break
            // Note: 100 means 0.1 seconds, so you shouldn't care about it
        }
    }
}

Finally your startThread and stopThread:

public void startThread() {
    threadRunning = true;
    thread = new Thread(threadRunnable);
    thread.start();
}
public void stopThread() {
    threadRunning = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        // Error at joining the thread
        // Thread JOIN means that no other thread may start before this one ends
        e.printStackStrace();
    }
}
Community
  • 1
  • 1
Chaoz
  • 2,119
  • 1
  • 20
  • 39
  • Thanks for the answer, actually I wish to get the details every seconds, but is it possible? I have edited my question and provide the code I tried, there may be mistake in the code. – Siang May 09 '15 at 16:57
  • Sorry for my late reply. Anyway I updated my answer, should be good now. – Chaoz May 13 '15 at 16:57
  • @Siang this is a comment only to notify you I updated, I forgot the @ Siang in the top comment – Chaoz May 13 '15 at 16:58