Wifi Scanner which scans 20 times

前端 未结 2 592
迷失自我
迷失自我 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 wifilist = wifi.getScanResults();
                  data = new scan_data[wifilist .size()];
    
                  int a =0;
    
                 // for (int i=0;i<20;i++){ //DELETE THE LOOP
                      if (a

    check this sample, just add the if(TOTAL_SCANS<20) at end on onReceive()

提交回复
热议问题