3

我正在尝试使用类 WifiManager (StartScan) 扫描一些接入点,此扫描发生在触摸事件中,但每次触摸仅进行一次扫描。问题是我需要将此过程设为 10、20 或我想要的次数;但是我不知道我该怎么做。因为我认为一个 for 循环就足够了,但是这种方法对我不起作用。

这是我的 onTouch 方法,我需要每次触摸屏幕时重复 10 次

public boolean onTouch(View v, MotionEvent event) { 
// TODO Auto-generated method stub 
WifiManager w = (WifiManager) getSystemService(Context.WIFI_SERVICE);
texto.setLength(0); 
switch (event.getAction()){ 
case MotionEvent.ACTION_DOWN:
texto.append("down"; posx:(double) event.getX(); posy:(double) event.getY();    
w.startScan(); 
break;
}
return false; 
}

我尝试了下一个 for 循环,但没有用

for(int i=1;i <= 10;i++){ 
w.startScan();
}
4

2 回答 2

1

You need to request the scan and then wait for the results in a BroadcastReceiver , something like this:

        BroadcastReceiver receiver_aps = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
               List<WifiConfiguration> results = wifiManager.getConfiguredNetworks();
               List<ScanResult> scanResults = wifiManager.getScanResults();   
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(receiver_aps,filter);  

When the Scan has finished you can then request a scan again (in the BroadcastReceiver) keeping a counter to make sure you only do it ten times.

Your current code is requesting 10 scans within the time it takes to complete the initial scan so all subsequent scan requests are ignored.

于 2012-10-30T18:32:45.357 回答
0

扫描是一个需要很长时间才能完成的命令。扫描时忽略其他扫描命令。

scan 在所有 2.4GHz 和 5GHz 通道(一次一个)中发送探测并等待答案。
为什么需要连续发出 10 次扫描?

于 2012-10-30T18:21:01.177 回答