在Android中的Wifi扫描

时间:2013-12-31 21:16:07

标签: android android-wifi wifimanager

我的问题似乎很常见,但我不是在寻找代码。如果任何人能够解释扫描是如何工作的,我将不胜感激。具体来说,我写了一个只调用wifi.scan一次的代码,但我看到的是它不断打印扫描结果。这是为什么?调用wifi管理器的扫描方法会导致它继续显示结果吗?

如果我想进行多次扫描,一次又一次扫描会多次调用扫描吗?

这是我的代码,以防问题出现在我的代码中,而不是扫描方法的工作方式。

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

1 个答案:

答案 0 :(得分:1)

扫描结果会连续打印,因为一旦执行wifi.scan(),Android会每隔15秒继续扫描并向您发送广播意图(不确定15是否适用于所有设备)。因此,无需多次致电scan()