0

This is my code below and it works fine, when i walk into a wifi enviroment im able to receive all the wifi BSSID and when i walk out, it returns null. however i saw examples that uses broadcast receiver, isit nescessary for wifi scanning? Just want to make sure im doing it right

 public static String getBSSID(Context context){ /
    WifiManager wifiManager;
    List<ScanResult> results;
    List<String> ids = new ArrayList<>();
    wifiManager = (WifiManager)  context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    String essidPrefix = MainFragment.configuration.getEssidPrefix();
    String bssid = null;

    results = wifiManager.getScanResults();
    for(ScanResult scanResult : results){
        String scanWifi = scanResult.SSID;
       if(scanWifi.startsWith(essidPrefix)){
          ids.add(scanResult.BSSID);
          bssid = TextUtils.join(",",ids);


       }

    }

    StatusActivity.addMessage(bssid);
    return bssid;
shizhen
  • 12,251
  • 9
  • 52
  • 88
Joshua Tan
  • 13
  • 5

1 Answers1

0

As per android documentation, you register a broadcast listener for SCAN_RESULTS_AVAILABLE_ACTION, which is called when scan requests are completed, providing their success/failure status. https://developer.android.com/guide/topics/connectivity/wifi-scan So in case you want to handle your success and failure result.

As for using the getScanResult() goes, the docs clearly mentions that the returned scan results are the most recently updated results, which may be from a previous scan if your current scan has not completed or succeeded. This means that you might get older scan results if you call this method before receiving a successful SCAN_RESULTS_AVAILABLE_ACTION broadcast. So this is the reason why you need BroadcastReceiver and in case of not using it you might see stale or not current results.

MXC
  • 458
  • 1
  • 5
  • 21