Android Scan for Wifi networks

前端 未结 5 709
北荒
北荒 2021-02-02 04:33

I\'m trying to scan for wireless networks and found this helpful source on the net. Unfortunately it\'s not working and I have no idea why. My problem is that I can\'t wait 10 m

相关标签:
5条回答
  • 2021-02-02 05:08

    You should write in BroadcastReceiver like this:

    1. Register it
    2. Then startScan and do like this

      if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
      
      super.onReceive(context, intent);           
      //Scan is ok, just need few seconds!
      }
      
    0 讨论(0)
  • 2021-02-02 05:09

    Ok, I found the mistake.

    It was the loop. It looks like the onReceive function is never called as the activity run this loop only. Looks like the program has to reach the end of the function to execute other function like OnReceive ...

    Thanks for the help any way. It helped me to improve it a bit :)

    0 讨论(0)
  • 2021-02-02 05:12

    Where are you putting this code? In the onCreate of an activity?

    The problem is that you're registering a callback which will get called when you receive the scan results, which according to the Android API docs is done in a separate thread, so your busy-waiting loop is achieving nothing in this circumstance except needlessly halting your activity, and if it's during the onCreate that means it never exits the method.

    0 讨论(0)
  • 2021-02-02 05:15

    Well i dont know anything about speeding up the process, it could just be that it takes a while to find the wifi signals (that, or your wifi is not turned on... which is something that your program should check for before it starts). However, one thing you can do to improve your workflow would be to do all of this in a different activity using startActivityForResult(). That way your "main" activity will be able to act on that data after it's done and you wont have to eat up the cpu on a while loop.

    public void onActivityResult(....){
       switch(retCode){
       case SCAN_ACTIVITY:{
             //do stuff
          }
       }
    }
    
    0 讨论(0)
  • 2021-02-02 05:17

    you need to implement a BroadcastReceiver listening for the scan results returned from WifiManager.startScan(). onReceive() allows you to access the scan resuls directly. it takes about 1 second for the scan to complete and trigger onReceive()...

    0 讨论(0)
提交回复
热议问题