检查startScan是否成功

时间:2013-07-26 10:50:52

标签: android wifi wifimanager android-1.6-donut

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

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

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

P.S:

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

}

1 个答案:

答案 0 :(得分:0)

您正在做的是在每个循环周期开始扫描两次。我想你想要的是:

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

请注意,如果您不在后台线程中执行此代码,则可能会阻止UI线程。 此外,startScan返回true并不表示任何新的扫描结果可用。 为此,您必须为BroadcastReceiver注册WifiManager.SCAN_RESULTS_AVAILABLE_ACTION