I am trying to make wifi scanner for my project which does 20 scan when I hit the scan button. When I run the code its scans but I dont know it scans for 20 times or not and the
your wifi scans 20 times, apparently because of this
for (int i=0;i<20;i++){
wifi.startScan();
your are starting scan 20 times inside the for loop, why?
and regarding unregister the receiver,
it can be placed at onDestroy()
one more note, the line
wifi.startScan();
should not be placed inside the onReceive()
it should be placed at onClick() of scan buttom
and onReceive()
will be called so you can get data and save it
so if you call startScan it will trigger scan again
and you are using too many hardcoded sizes , 100 and 20 what are these suppose to be?
Edit:
scan_data[] data = null;
int TOTAL_SCANS = 0;
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifilist = wifi.getScanResults();
data = new scan_data[wifilist .size()];
int a =0;
// for (int i=0;i<20;i++){ //DELETE THE LOOP
if (a<wifilist.size()){
a=wifilist.size();
}
for(int r=0;r<wifilist.size();r++){
data[r] = new scan_date();
data[r].ssd=wifilist.get(r).ssd();//check syntax here ... just sample
}
//at end of onReceive()
if(TOTAL_SCANS < 20){
++TOTAL_SCANS;
wifi.startScan();
}else{
System.out.println(" Scan Completed [" + TOTAL_SCANS + "] times.");
//you can unregisterReceiver here too
}
:
:
check this sample, just add the if(TOTAL_SCANS<20)
at end on onReceive()
Update: You can take a look at my demo for RF measurements using Android device, where you can found a couple of approaches, here:
https://github.com/panosvas/Measurements
where you can find an implementation of WiFi measurements one after another. I have also created a Server for storing these measurements as well as a remote trigger app using UDP packets where you can find here:
https://github.com/panosvas/IndoorPositioningServer
It is possible to obtain multiple measurements. Your approach is wrong due to the fact that the for loop will be executed probably in mseconds and each wifi measurement in Android keeps approximately 5 seconds based on the device. One approach to achieve your goal is the following:
Keep in mind that in order to obtain correct measurements you have to wait until each measurement finishes otherwise you will take duplicates of the same measurement.