Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 426
Nirmal Avatar asked Jul 23 '18 06:07

Nirmal


People also ask

How do I find the BSSID for my wireless network?

If you're looking for your BSSID on the Windows OS, you can run the command netsh wlan show interfaces | find “BSSID”. On macOS, you can hold the Option key down while you're also clicking on the WiFi icon which is found in the top right-hand corner. The drop-down menu will have the BSSID listed automatically.

How is BSSID generated?

The BSSID is a 48bit identity used to identify a particular BSS (Basic Service Set) within an area. In Infrastructure BSS networks, the BSSID is the MAC (Medium Access Control) address of the AP (Access Point) and in Independent BSS or ad hoc networks, the BSSID is generated randomly.

What is the BSSID of my router?

Known as a "Basic Service Set Identifier," the BSSID is basically the MAC physical address of the wireless router or access point the user is using to connect via WiFi.

What is multi BSSID?

The Multiple BSSID element (ID 71) allows an AP to collapse information for collocated networks running on the same Wi-Fi channel into a single beacon or probe response frame. It avoids sending the same information elements (e.g., Supported Rates, HE Capabilities, HE Operation, etc.)


Video Answer


2 Answers

I have also got the same problem. I solved it by the help of broadcast receiver and build my own logic around it.

Broadcast Receiver class, make sure for provided permissions ACCESS_WIFI_STATE and CHANGE_WIFI_STATE in manifest.

public class WifiChecker extends BroadcastReceiver {

    private OnWifiResultArrived onWifiResultArrived = null;
    private static boolean CAN_CALL_AGAIN = true;
    private WifiManager wifiManager;

    /**
     * @param context context of activity.
     * Remember to provide permission
     * <p>
     * {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />},
     * {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />}</p>
     */
    @SuppressLint("MissingPermission")
    public WifiChecker(Context context) {
        CAN_CALL_AGAIN = true;
        wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        context.registerReceiver(this, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();

        rerunAgain();
    }

    private void rerunAgain() {
        new Handler().postDelayed(new Runnable() {
            @SuppressLint("MissingPermission")
            @Override
            public void run() {
                if (CAN_CALL_AGAIN)
                    wifiManager.startScan();

                rerunAgain();       //rerun the broadcast again
            }
        }, 1000);
    }

    public void addListerForWifiCallback(OnWifiResultArrived onWifiResultArrived) {
        this.onWifiResultArrived = onWifiResultArrived;
    }


    @SuppressLint("MissingPermission")
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUi(wifiManager.getScanResults());
    }

    private void updateUi(final List<ScanResult> scanResults) {

        try {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (onWifiResultArrived != null)

                            onWifiResultArrived.isInWifiRange(scanResults);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }, 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void unregisterListner(Context context) {
        this.onWifiResultArrived = null;
        CAN_CALL_AGAIN = false;
    }

    public interface OnWifiResultArrived {
        void isInWifiRange(List<ScanResult> scanResults);
    }
}

User of Broadcast class Either implement the broadcast receiver class interface i.e.,OnWifiResultArrived

WifiChecker checker = new WifiChecker(this);
checker.addListerForWifiCallback(this);

@Override
public void isInWifiRange(List<ScanResult> scanResults){
    //get your BSSID here
    scanResults.get(position).BSSID;
    //write your logic for checking weather it is connected or not
}

or

WifiChecker checker = new WifiChecker(this);
checker.addListerForWifiCallback(@Override
public void isInWifiRange(List<ScanResult> scanResults){
    //get your BSSID here
    scanResults.get(position).BSSID;
   //write your logic for checking weather it is connected or not
});
like image 53
Ranjan Avatar answered Oct 03 '22 10:10

Ranjan


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();
like image 42
ashish kadam Avatar answered Oct 03 '22 08:10

ashish kadam