How to get BSSID of all wifi access points?

2020-02-26 06:57发布

问题:

I need to get BSSID(MAC) of all AP's. Please find below code block.

    List<WifiConfiguration> test =  wifiManager.getConfiguredNetworks();
    for(int k=0;k<test.size();k++){ 
        Log.d("acheck", "test BSSID = "+test.get(k).BSSID);         
        Log.d("acheck", "test BSSID = "+test.get(k).BSSID);    
   }

But above code block returns SSID properly but null for BSSID. I want to connect to strongest access point with user defined SSID. Is there any API method available to get best signal strength's AP with user defined SSID.(In my case there are many SSID's with single SSID with multiple AP's available.)

回答1:

Adapted the answer by @ToonSuperLove, mainly to avoid NPEs.

public class WifiTest extends Activity {

    public void loadWifiAvailableList(EditText etWifiList) {
        WifiManager wifiManager = (WifiManager) 
                                    getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> results = wifiManager.getScanResults();
        String message = "No results. Check wireless is on";
        if (results != null) {
            final int size = results.size();
            if (size == 0) message = "No access points in range";
            else {
                ScanResult bestSignal = results.get(0);
                etWifiList.setText(""); // etWifiList is EditText
                int count = 1;
                for (ScanResult result : results) {
                    etWifiList.append(count++ + ". " + result.SSID + " : "
                            + result.level + "\n" + result.BSSID + "\n"
                            + result.capabilities + "\n"
                            + "\n=======================\n");
                    if (WifiManager.compareSignalLevel(bestSignal.level,
                            result.level) < 0) {
                        bestSignal = result;
                    }
                }
                message = String.format(
                        "%s networks found. %s is the strongest.", size,
                        bestSignal.SSID + " : " + bestSignal.level);
            }
        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
}


回答2:

In configuration it just the setting part (eg. like the dialog that ask user/pass/ WPA or NULL blah blah)

To get BSSID from AP, you need to get it from ScanResult

public void loadWifiAvailableList() {
    List<ScanResult> results = wifiManager.getScanResults();
    ScanResult bestSignal = null;
    etWifiList.setText(""); // etWifiList is EditText
    int count = 1;
    for (ScanResult result : results) {
        etWifiList.append(count++ + ". " + result.SSID + " : " + result.level + "\n" +
                result.BSSID + "\n" + result.capabilities + "\n" +
                "\n=======================\n");

      if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0) 
      {
        bestSignal = result;
      }
    }


    String message = String.format("%s networks found. %s is the strongest.",
        results.size(), bestSignal.SSID + " : " + bestSignal.level);
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}


回答3:

To get BSSID for currently connected WIFI network, use WiFiInfo class.

            WifiManager wifiMan = (WifiManager) context.getSystemService(
                            Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiMan.getConnectionInfo();

            String macAddr = wifiInfo.getMacAddress();
            String bssid = wifiInfo.getBSSID();