gpt4 book ai didi

android - android中的Wifi扫描

转载 作者:行者123 更新时间:2023-11-30 03:11:34 24 4
gpt4 key购买 nike

我的问题可能看起来很常见,但我不是在寻找代码。如果有人能解释扫描的真正工作原理,我将不胜感激。具体来说,我写了一段代码,只调用了一次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();
}
}
}

最佳答案

扫描的结果是连续打印的,因为一旦你执行了 wifi.scan(),Android 就会持续扫描并每 15 秒向你发送一次广播 intent(不确定 15 是否代表所有设备)。因此,无需多次调用 scan()

关于android - android中的Wifi扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com