如何让所有的WiFi接入点的BSSID?(How to get BSSID of all wifi

2019-07-31 16:00发布

我需要BSSID(MAC)的所有AP的。 请从下面的代码块。

    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);    
   }

但上面的代码块返回SSID正常,但空的BSSID。 我想与用户定义的SSID连接到最强的接入点。 是否有任何可用的API方法来获取信号强度最好的AP与用户定义的SSID。(在我的情况有很多SSID与单个SSID与多个AP的使用。)

Answer 1:

适于通过@ToonSuperLove答案,主要是为了避免NPE上。

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();
    }
}


Answer 2:

在配置它只是设定部件(例如像询问用户/通行证/ WPA或NULL等等等等的对话框)

为了从AP获得BSSID,你需要从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();
}


Answer 3:

要获得BSSID当前连接WIFI网络,使用WiFiInfo类。

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

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


文章来源: How to get BSSID of all wifi access points?