Android 如何获取所有wifi接入点的BSSID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12460445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:43:38  来源:igfitidea点击:

How to get BSSID of all wifi access points?

androidwifiandroid-wifiwificonfiguration

提问by Balaji Khadake

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

我需要获取所有 AP 的 BSSID(MAC)。请找到下面的代码块。

    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.)

但上面的代码块正确返回 SSID,但为 BSSID 返回 null。我想使用用户定义的 SSID 连接到最强的接入点。是否有任何 API 方法可用于通过用户定义的 SSID 获得最佳信号强度的 AP。(在我的情况下,有许多具有单个 SSID 的 SSID,而多个 AP 可用。)

回答by Mr_and_Mrs_D

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

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

回答by Sruit A.Suk

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

在配置中,它只是设置部分(例如,像询问用户/通过/ WPA 或 NULL 等等的对话框)

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

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

回答by Ray

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

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

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

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