[英]How can I receive new scan results in my android phone every time I press a button?
I am creating an app that needs to scan for surrounding access points and according to the results do some calculations. 我正在创建一个需要扫描周围接入点的应用程序,并根据结果进行一些计算。 By now I am convinced that the
WifiManager.statScan()
method although it initiates a new scan, it doesn't automatically return results from a new scan. 到目前为止,我已经确信
WifiManager.statScan()
方法虽然启动了新的扫描,但不会自动返回新扫描的结果。
This is my code: 这是我的代码:
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
//do some reseting for the GUI and the values of the problem.
break;
case R.id.button2:
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
List<ScanResult> scanResult = null;
if(wifiManager.startScan()){
scanResult = wifiManager.getScanResults();
}
// Do the calculations.
break;
}
}
My problem is that I am not sure if using this code I actually get the results from the initiated scan or the results coming from previous scans, and if the latter is correct, how can I get the results from the new scan? 我的问题是我不确定是否使用此代码从启动的扫描中实际获取了结果,还是从先前的扫描中获取了结果,如果后者正确,如何从新的扫描中获取结果?
Thank you very much. 非常感谢你。
After searching and reading various posts and tutorias, my code has transformed as follows: 在搜索并阅读各种帖子和文摘之后,我的代码进行了如下更改:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
BroadcastReceiver receiver;
private List<ScanResult> scanResult;
boolean waiting;
// and other irrelevant variables.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization of the elements of the GUI
reset.setOnClickListener(this);
locate.setOnClickListener(this);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
waiting = true;
receiver = new WifiScaner(this);
registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
// resetting the variables of the application
break;
case R.id.button2:
wifiManager.startScan();
while (waiting) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("PROJECT1","Wifi WAITING");
}
//Calculations
break;
}
}
public class WifiScaner extends BroadcastReceiver{
MainActivity main;
public WifiScaner(MainActivity main){
super();
this.main = main;
}
public void onReceive(Context c, Intent intent) {
scanResult = main.wifiManager.getScanResults();
waiting = false;
Log.d("PROJECT1","Wifi RECEIVED");
}
}
} But for some reason it seems that I never get into the onReceive of WifiScaner. }但是由于某种原因,我似乎从未涉足WifiScaner的onReceive。 Any clues why this is happening?
任何线索为什么会这样?
You are right, a scan takes some time so results are not available immediately. 没错,扫描需要一些时间,因此无法立即获得结果。 This is confirmed by the doc for startScan() method:
这已由doc的startScan()方法确认:
"Request a scan for access points. Returns immediately. The availability of the results is made known later by means of an asynchronous event sent on completion of the scan." “请求扫描访问点。立即返回。稍后将通过在扫描完成时发送的异步事件来告知结果的可用性。”
The following link seems to have a good example of how to be notified when the scan completes: http://www.tutorialspoint.com/android/android_wi_fi.htm . 以下链接似乎是一个很好的示例,说明如何在扫描完成时得到通知: http : //www.tutorialspoint.com/android/android_wi_fi.htm 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.