How to get BSSID of all wifi access points?

一个人想着一个人 提交于 2019-11-30 08:41:00

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

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

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!