androidandroid-wifibssid

how to get BSSID of all connected network?


i have used the below code but it was working well but after some month i am getting a result as any instead of getting BSSID value. here is my code. please guide me any other alternative way.

 @SuppressLint("LongLogTag")
public void loadWifiAvailableList() {
    WifiManager wifiMan = (WifiManager) getApplicationContext().getSystemService(
            Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMan.getConnectionInfo();

    String macAddr = wifiInfo.getMacAddress();
    String bssid = wifiInfo.getBSSID();
   //here i am getting the proper bssid
    Log.d("bssid from get connection info",bssid);

    List<WifiConfiguration> list = wifiMan.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.BSSID!=null)
     //here i am getting any from i.BSSID
        Log.d("bssid from get configured network",i.BSSID);

    }
}

enter image description here


Solution

  • I have found out some solution why am i getting any or null string in getConfiguredNetworks() api in android. the reason behind is if we have the same SSID and same password at some scenario it is sending as any or null string. so getConfiguredNetworks() is not a right method when you are trying to connect with same ssid. so use getScanResults() Api instead of getConfiguredNetworks(). After some own research i have found it out. This problem will occur only when you get an AP list with same ssid and password.use the scan result and add that network in wifi configuration then connect it but this method was deprecated in API level 26. so it wont work from api level 26. reference link

      //https://stackoverflow.com/a/8818490
            wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    
            //checking the right network.
            List<ScanResult> scanresults = wifiManager.getScanResults();
            {
                for (ScanResult scanresult : scanresults) {
                    Log.d("scan result1", scanresult.BSSID + "");
     }}
    
    
     WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";
        conf.BSSID = Bssid;
      WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      ///this wont work from api level 26
        wifiManager.addNetwork(conf);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {
            if (i.BSSID != null && i.BSSID.equals(Bssid)) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reassociate();
                Log.d("changing network", "connecting the right network");
                break;
            }
    
        }