How to get BSSID of all wifi access points?
Asked Answered
B

3

13

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

Bruni answered 17/9, 2012 at 13:37 Comment(0)
C
12

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();
    }
}
Caramel answered 22/4, 2013 at 0:6 Comment(2)
What is NPE? ​ ​ ​ ​Clayton
@Clayton I guess it's NullPointerExceptionSelfsufficient
C
7

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();
}
Cockatiel answered 27/9, 2012 at 4:22 Comment(1)
adapted this excellent snippet and mixed it with some code from Marakana (grazie mille, marko!) to extract everything from connection and wifi network info store on device. :) UPVOTED! https://mcmap.net/q/112757/-how-to-get-name-of-wifi-network-out-of-android-using-android-apiHonea
E
1

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();
Eyeless answered 19/8, 2014 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.