To check if wifi scan is complete

I am writing code to continuously obtain information about all access points in a range.

Here is my code:

myRunnable = new Runnable() { @Override public void run() { while(){ wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifi.startScan(); List<ScanResult>results= wifi.getScanResults(); try{ //data Logging } } } }; myThread = new Thread(myRunnable); myThread.start(); 

With continuous scanning and data logging, I want to check if the scan is completed before the data is recorded. Is there any flag or function that checks that wifi.startScan () is complete or not, before registering the data. Could you help me with the code.

+4
source share
3 answers

I want to check if the scan is complete before data logging. Is there any flag or function that checks wifi.startScan (), is complete or not, before registering data. Could you help me with the code.

Yes, a mechanism is provided for this. To achieve your goal, you need to implement BroadcastReceiver , which will listen to WiFi scans.

All you need to do is create a BroadcastReceiver with the corresponding IntentFilter . So you need this action for the filter:

 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION 

This action means that the access point verification is complete and the results are available. And then just create a BroadcastReceiver (statically or dynamically, it doesn't matter).

If you do not know how to start, look at this tutorial:

+4
source

You need to implement BroadcastReceiver listening on scan results returned from WifiManager.startScan() . onReceive() allows you to directly receive scan results. It takes about 1 second to complete the scan and onReceive() starts ...

+1
source

To work out the answers above, I just wanted to add related code to give an example.

1. Registration for the event

 // don't forget to set permission to manifest wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); context.registerReceiver(this,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); //check if WIFI is enabled and whether scanning launched if(!wifiManager.isWifiEnabled() || !wifiManager.startScan()) { throw new Exception("WIFI is not enabled"); } 

2. Getting the result

To get an object, the registered object (in this case, this) must extend BroadcastReceiver and implement the following method:

 public void onReceive(Context context, Intent intent) { List<ScanResult> result = wifiManager.getScanResults(); } 
+1
source

Source: https://habr.com/ru/post/1491401/


All Articles