简体   繁体   English

每次按下按钮时,如何在Android手机中接收新的扫描结果?

[英]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.

相关问题 Android Studio-每次单击按钮时如何在线性布局上生成新的textview - Android Studio - How can I generate a new textview on my linear layout every time i click a button 如何在 Android 中使用手机传感器扫描我的指纹? - How can i scan my fingerPrint using phone sensor in Android? 如何在android中检测按钮的时间按下持续时间? - How can I detect time press duration of button in android? 每次按下“ Android Studio”按钮时都会崩溃 - Getting a crash every time i press a button“Android Studio” 如何防止我的Android应用在每次手机进入睡眠状态时重置? - How do I prevent my Android app from resetting every time my phone sleeps? Android编程:每次按下屏幕上的按钮时,如何在屏幕上添加文本视图? - Android Programming: How can I add a text view on the screen each time I press a button on the screen? 如何在我的android应用程序中使用传感器接收电话? - How Can I receive a Phone call using sensor with my android application? 如何将Android Wifi扫描结果放入列表? - How can I get Android Wifi Scan Results into a list? 我每次移动Android手机时如何动态获取gps值? - how to dynamically get valuse of gps every time i move my android phone? 如果按下按钮,如何更改页面? (Android,ViewPager) - How can I change the page if I press the button? (Android, ViewPager)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM