Reputation: 541
My question might seem very common, but I'm not looking for code. I would appreciate it if any one can explain how scanning really works. To be specific, I wrote a code that calls wifi.scan
one time only, but what I see is that it keeps printing the result of scan continuously. Why is that? Does calling scan method of the wifi manager cause it to keep displaying the results?
In case that I want to do more than one scan, one scan after another will calling scan multiple time will do it?
Here is my code, in case the problem is in my code not the way scan method works.
public class FingerPrint extends Activity {
List<ScanResult> wifiList;
WifiManager Wifi;
StringBuilder sb = new StringBuilder();
Button fingerPrint;
Integer locationID;
WifiReceiver receiverWifi;
Toast toast;
Context context;
TextView t1;
Integer ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context= this;
setContentView(R.layout.activity_finger_print);
t1=(TextView)findViewById(R.id.t1);
Wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
fingerPrint = (Button)findViewById(R.id.fingerprint);
fingerPrint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.locationid, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(promptView);
final EditText input = (EditText) promptView.findViewById(R.id.userInput);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to resul
String temp =input.getText().toString();
ID = Integer.valueOf(temp);
toast = Toast.makeText(context,"Scanning started" , Toast.LENGTH_SHORT);
toast.show();
//create file
Wifi.startScan();
// making fingerprint
// we will take 10 RSS at each location and average them
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
}
}); // on click
}//on create
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.finger_print, menu);
return true;
}
protected void onPause() {
unregisterReceiver(receiverWifi);
super.onPause();
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
sb = new StringBuilder();
wifiList = Wifi.getScanResults();
sb.append("Location : "+ID);
for(int i = 0; i < wifiList.size(); i++){
sb.append("\nMAC : "+(wifiList.get(i)).BSSID.toString());
sb.append("RSS : " +(wifiList.get(i)).level+" -dBm");
sb.append("\n");
}
t1.setText(sb.toString());
// toast=Toast.makeText(context,sb.toString() , Toast.LENGTH_LONG);
// toast.show();
}
}
}
Upvotes: 1
Views: 569
Reputation: 81
The results of a scan are printed continuously, because once you do wifi.scan()
, Android keeps scanning and sending you broadcast intent for it every 15 seconds (not sure whether 15 is for all devices). So, there is no need to call scan()
multiple times.
Upvotes: 1