I'm writing a code for getting the information of all the access points in the range continuously.
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();
As the scanning and logging of data continuous repeatedly, i want to check whether the scan is complete before logging data. Is there any flag or function which checks for wifi.startScan() is complete or not , before logging the data. Could you please help me out with a code.
i want to check whether the scan is complete before logging data. Is there any flag or function which checks for wifi.startScan() is complete or not , before logging the data. Could you please help me out with a code.
Yes, there is mechanizm designated for this purpose. To reach your goal you need to implement BroadcastReceiver
that will listen for WiFi
scans.
All what you need is to create BroadcastReceiver
with appropriate IntentFilter
. So you need this action for filter:
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
This action means that an access point scan has completed, and results are available. And then just create BroadcastReceiver
(statically or dynamically, it's doesn't matter).
If you don't know how to start, look at this tutorial:
To build up on the answers above, I just wanted to add the related code to give an example.
1. Registering 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. Receiving the result
To receive, the object registered (in this case this) needs to extend BroadcastReceiver
and implement the following method:
public void onReceive(Context context, Intent intent) {
List<ScanResult> result = wifiManager.getScanResults();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With