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