0

我想检查方法 wifiManager.startScan() 是否有效地成功进行 AP 扫描。如果开始已经开始,则此方法返回 true,但如果扫描成功则不返回。事实上,即使 wifiManager.startScan() 返回 true,我有时也会在 LogCat 中收到“无法启动 AP 扫描”的消息……那么当我在 logcat 中有此消息时,如何重新开始新的扫描?

这是我当前代码的一部分:

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

PS:

我的 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;
        }

}
4

1 回答 1

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.

于 2013-07-26T11:20:30.913 回答