android中的Wifi扫描

android中的Wifi扫描,android,android-wifi,wifimanager,Android,Android Wifi,Wifimanager,我的问题似乎很常见,但我不是在寻找代码。如果有人能解释一下扫描是如何工作的,我将不胜感激。具体来说,我写了一个只调用一次wifi.scanwifi.scan的代码,但我看到的是它不断打印扫描结果。为什么呢?wifi manager的调用扫描方法是否会导致其继续显示结果 如果我想做一次以上的扫描,一次又一次的扫描会多次调用扫描吗 这是我的代码,以防问题出在我的代码中,而不是扫描方法的工作方式 public class FingerPrint extends Activity { List

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

如果我想做一次以上的扫描,一次又一次的扫描会多次调用扫描吗

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

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();
            }
        }
}
公共类指纹扩展活动{
列表wifiList;
WifiManager-Wifi;
StringBuilder sb=新的StringBuilder();
钮扣指纹;
整数位置ID;
无线接收器接收器;
吐司;
语境;
文本视图t1;
整数ID;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
上下文=这个;
setContentView(R.layout.activity\u finger\u print);
t1=(TextView)findViewById(R.id.t1);
Wifi=(WifiManager)getSystemService(Context.Wifi\u服务);
receiverWifi=新WifiReceiver();
registerReceiver(接收方WiFi,新的意向过滤器(WifiManager.SCAN\u RESULTS\u AVAILABLE\u ACTION));
指纹=(按钮)findViewById(R.id.fingerPrint);
fingerPrint.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
LayoutInflater LayoutInflater=LayoutInflater.from(上下文);
View promptView=LayoutFlater.inflate(R.layout.locationid,null);
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(上下文);
alertDialogBuilder.setView(promptView);
最终EditText输入=(EditText)promptView.findViewById(R.id.userInput);
alertDialogBuilder.setCancelable(false)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
//获取用户输入并将其设置为resu
String temp=input.getText().toString();
ID=整数。值(温度);
toast=toast.makeText(上下文,“扫描开始”,toast.LENGTH\u SHORT);
toast.show();
//创建文件
Wifi.startScan();
//制作指纹
//我们将在每个位置获取10个RSS,并对其进行平均
}
})
.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});
AlertDialog alertD=alertDialogBuilder.create();
alertD.show();
}
});//单击
}//论创造
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单指纹,菜单);
返回true;
}
受保护的void onPause(){
未注册的接收器(接收器);
super.onPause();
}
类WifiReceiver扩展了BroadcastReceiver{
接收时公共无效(上下文c,意图){
sb=新的StringBuilder();
wifiList=Wifi.getScanResults();
sb.附加(“位置:+ID”);
对于(int i=0;i
扫描结果会连续打印,因为一旦您执行了
wifi.scan()
,Android会每隔15秒扫描并向您发送广播意图(不确定15是否适用于所有设备)。因此,无需多次调用
scan()