0

I would like to check if the method wifiManager.startScan() has effectively success to make an AP scan. This method returns true if the start has begun, but not if the scan has succeed. Indeed, I sometimes receive the message "Failed to initiate AP scan" in the LogCat even if wifiManager.startScan() returns true... So how can I restart a new scan when I have this message in the logcat?

This is a part of my current code :

while(wifiManager.startScan()==false){
wifiManager.startScan();
}

P.S:

The full code of my BroadcastReceiver :

package paquet.wifiview2;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class WifiBroadcastReceiver extends BroadcastReceiver {

    private WifiManager wifiManager;
    private WifiAdapter wifiAdapter;
    private List<WifiItem> listeWifiItem;
    private int position=0;
    private boolean start=true;
    private int numberdisplay=4;
    private float[][] valueRSSI = new float[5][10];
    private String[] macAdress = new String[5];
    private int[] sampleposition = new int[5];
    private String[] sSID = new String[5];
    private int totalsample=50;
    private boolean copy=false;
    private static boolean broadcastFinish=true;
    //private boolean scanStart=false;


    @SuppressLint("ShowToast")
    @Override
    public void onReceive(Context context, Intent intent) {

        macAdress[0]="5c:0e:8b:26:d7:72";
        macAdress[1]="5c:0e:8b:26:d7:70";
        macAdress[2]="00:16:9c:92:04:30";
        macAdress[3]="00:1c:df:7f:6b:85";
        macAdress[4]="5c:0e:8b:21:81:12";

        Log.d("info3","broadcastFinish (Receiver) : " + broadcastFinish);


        wifiManager = ((WifiActivity) context).getCurrentWifiManager();
        wifiAdapter = ((WifiActivity) context).getWifiAdapter();
        listeWifiItem = ((WifiActivity) context).getListeWifiItem();
        if(position>numberdisplay){position=0;}

        // Check if the object is well instantiated
        if (wifiManager != null) {

            // Check if wifi is turned on
            if (wifiManager.isWifiEnabled()) {


                List<ScanResult> listeScan = wifiManager.getScanResults();// Getting the scan
                if (sampleposition[numberdisplay-1]==totalsample || start==true){ //If the number of samples is equal to the wished number

                    // For each scan

                            if(start==false && position<=numberdisplay-1){ // If you don't initialize and if you haven't real all the AP 
                                if(position == 0){copy=true;listeWifiItem.clear();} // If you start to read your list, you have to delete your old list before
                                if(copy==true){ //If you start to read your list effectively
                                    while(position<=numberdisplay-1){ // While you haven't read all the AP.
                                        if(sampleposition[position]!=0 && (position == 0 || macAdress[position]!=macAdress[0] && macAdress[position]!=macAdress[position-1])){ 
                                            //If the sample quantity of a position isn't null and if you haven't copy the concerned mac address
                                            for(int i=0;i<=1;i++){
                                                WifiItem item = new WifiItem();
                                                item.setAdresseMac(macAdress[position]);
                                                if(i==1){item.setAPName("old : " + sSID[position]);} else{item.setAPName(sSID[position]);}
                                                item.setForceSignal((valueRSSI[position][i])/(sampleposition[position]));
                                                Log.d("Info1",sSID[position] + " | macAdress[" + position + "] : " + macAdress[position] + " - RSSI : " + (valueRSSI[position][i])/(sampleposition[position]) + " dBm - echantillons : " + (sampleposition[position]));
                                                listeWifiItem.add(item);
                                            }
                                        }
                                    sampleposition[position]=0; //réinitialisation du nombre d'échantillon par position
                                    position++;//Incrementation of the readed position
                                    }
                                }
                            }
                            else{ //Initialization of WifiItem
                                while(position<=numberdisplay-1) { //You read all the scanned AP
                                    for(int i=0;i<=1;i++){
                                    WifiItem item = new WifiItem();
                                    item.setAdresseMac("00:00:00:00:00");
                                    item.setAPName("Initalisation");
                                        if (start == true){item.setForceSignal(1);} //Si on initalise bien, on affiche juste 1
                                    listeWifiItem.add(item);
                                    }
                                    for(int i=0;i<=9;i++){valueRSSI[position][i]=0;}//réinitialisation de la RSSI par position
                                    sampleposition[position]=0;//réinitialisation du nombre d'échantillon par position
                                    position++;//Incrémentation de la position lue

                                }
                            }
                            if(position==numberdisplay){start=false;position=0;} //If you have exceed the wanted number of displayed points, you go back to 0 and don't start.

                // Refreshing of the list
                    copy=false;
                    wifiAdapter.notifyDataSetChanged();
                }

                else { 
                    if(position <=numberdisplay-1){ //If you haven't exceed the wanted number of positions
                        for (ScanResult scanResult : listeScan) {
                            broadcastFinish = false;
                            if(sampleposition[position]==0){ //If you haven't start to sample
                                if(macAdress[position].equals(scanResult.BSSID)){ //Initialisation réelle des valeurs des positions
                                    for(int i=9;i>=1;i--){valueRSSI[position][i] = valueRSSI[position][i-1];} //Saving the old values of the RSSI average
                                    valueRSSI[position][0] = scanResult.level; //Starting to make a new average of 50 samples
                                    sSID[position] = scanResult.SSID; 
                                    Log.d("WifiActivity", scanResult.SSID + " MAC : " + scanResult.BSSID + " LEVEL " + scanResult.level + " | macadress : " + macAdress[position] );
                                    if(valueRSSI[position][0]!=0){sampleposition[position]++;}
                                    break;
                                }
                            }
                            else{
                                if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){
                                    valueRSSI[position][0] = valueRSSI[position][0] + scanResult.level;
                                    Log.d("WifiActivity", scanResult.SSID + " MAC : " + scanResult.BSSID + " LEVEL " + scanResult.level + " | macadress : " + macAdress[position] );
                                    sampleposition[position]++;
                                    break;
                                }
                            }
                        }
                        broadcastFinish = true;

                    }
                    if(sampleposition[position]>=totalsample){position++;}
                }
            }
        }

        else {
            Toast.makeText(context, "Vous devez activer votre WiFi", Toast.LENGTH_SHORT);
        }
    }


    public static boolean getBroadcastFinish() {
           return broadcastFinish;
        }

}
lukronos
  • 75
  • 1
  • 11

1 Answers1

0

What you are doing is start scanning twice per cycle of your loop. I guess what you want is:

boolean success = false;
while(success == false){
  success = wifiManager.startScan();
}

Please note that you might block the UI thread if you don't perform this code in a background thread. Moreover, startScan returning true does not say that any new scan results are available. For that, you have to register a BroadcastReceiver for WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • I have ever registered a `BroadcastReceiver` for this : ` @Override protected void onResume() { registerReceiver(broadcastReceiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); super.onResume(); }` But when you say that I might block the UI thread, do you mean that I should wait such a time before to restart a new scan? – lukronos Jul 26 '13 at 13:45
  • No, I mean that the UI might get stuck if startScan fails for a long time for any reason. You should wait until the BroadcastReceiver receives an update and then start a new scan, btw. Did my answer help? – FD_ Jul 26 '13 at 15:37
  • Ok, thanks! But how can I wait until the BroadcastReceiver receives an update? I have tried to create a static method like this `public static boolean getBroadcastFinish() {return broadcastFinish;}` in BroadcastReceiver.java and get : `broadcastFinish = WifiBroadcastReceiver.getBroadcastFinish(); Log.d("info3","broadcastFinish (activity) : " + broadcastFinish); if(broadcastFinish == true){...}` in my MainActivity, but the code runs again even if I write `broadcastFinish = false;` when the BroadcastReceiver uses the scan... Sorry for my english. – lukronos Jul 29 '13 at 14:37
  • Please start a new question or add the full code of your BroadcastReceiver to your question. The formatting of a comment is horrible. – FD_ Jul 29 '13 at 14:43