简体   繁体   English

如何获得所有连接网络的BSSID?

[英]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. 我用下面的代码,但它运作良好,但一些一个月后我得到一个结果,因为任何的不是获取BSSID值。 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);

    }
}

在此输入图像描述

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. 广播接收器类,确保在清单中提供权限ACCESS_WIFI_STATECHANGE_WIFI_STATE

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 ie, OnWifiResultArrived Broadcast类的用户要么实现广播接收器类接口,即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
});

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

I have found out some solution why am i getting any or null string in getConfiguredNetworks() api in android. 我找到了一些解决方案为什么我在android中的getConfiguredNetworks() api中获得任何或空字符串。 the reason behind is if we have the same SSID and same password at some scenario it is sending as any or null string. 背后的原因是,如果我们在某些情况下具有相同的SSID和相同的密码,则它将作为任何或空字符串发送。 so getConfiguredNetworks() is not a right method when you are trying to connect with same ssid. 所以当你尝试连接相同的ssid时, getConfiguredNetworks()不是一个正确的方法。 so use getScanResults() Api instead of getConfiguredNetworks() . 所以使用getScanResults() Api而不是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 . 只有当您获得具有相同ssid和密码AP列表时才会出现此问题。使用扫描结果并在wifi配置中添加该网络然后连接它,但此方法在API级别26中已弃用。 so it wont work from api level 26 . 所以它不会从api 级别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;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM