Wifi Scanner which scans 20 times

前端 未结 2 558
迷失自我
迷失自我 2021-01-28 05:07

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

相关标签:
2条回答
  • 2021-01-28 05:21

    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()

    0 讨论(0)
  • 2021-01-28 05:31

    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:

    • Create an Asynch task or a task that calls the startScan()
    • Keep a counter and update it each time the onRecieve is triggered
    • If the counter does not reach the limit then inside the onRecieve call again the task
    • If the counter reaches the limit then inside the onRecieve you don't have to call again the task

    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.

    0 讨论(0)
提交回复
热议问题