如何使用特定的bssid android连接到wifi?

如何使用特定的bssid android连接到wifi?,android,connection,android-wifi,wifi,bssid,Android,Connection,Android Wifi,Wifi,Bssid,我正在创建一个Android应用程序,它允许您使用特定的BSSID连接到WiFi。我已经实现了扫描所有WiFi并在WiFi网络安全类型功能中进行WiFi配置的部分。我还使用特定的BSSID实现了与WiFi网络的连接 但我有一个问题:与特定BSSID的连接在安全网络中运行良好,但在开放网络中不起作用,我不知道为什么。事实上,这种连接到具有特定BSSID的开放网络在三星Galaxy S4或更新版本上有效,但在Galaxy S2和S3上无效。。。真奇怪。在S2和S3上,在连接过程中不考虑BSSID 这

我正在创建一个Android应用程序,它允许您使用特定的BSSID连接到WiFi。我已经实现了扫描所有WiFi并在WiFi网络安全类型功能中进行WiFi配置的部分。我还使用特定的BSSID实现了与WiFi网络的连接

但我有一个问题:与特定BSSID的连接在安全网络中运行良好,但在开放网络中不起作用,我不知道为什么。事实上,这种连接到具有特定BSSID的开放网络在三星Galaxy S4或更新版本上有效,但在Galaxy S2和S3上无效。。。真奇怪。在S2和S3上,在连接过程中不考虑BSSID

这是我用来为开放网络创建配置并尝试在其上连接特定BSSID的示例代码:

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + wifiItem.getSSID() + "\"";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    WifiManager wifiManager = (WifiManager) parentActivity.getSystemService(Context.WIFI_SERVICE);

    wifiManager.addNetwork(conf);
    conf.BSSID = wifiItem.getBSSID();
    wifiManager.updateNetwork(conf);
    wifiManager.saveConfiguration();

    for (WifiConfiguration wifiConfiguration : wifiManager.getConfiguredNetworks()) {

        if (wifiConfiguration.SSID.equals("\"" + wifiItem.getSSID() + "\"")) {

            wifiManager.disconnect();
            wifiConfiguration.BSSID = wifiItem.getBSSID();
            wifiManager.updateNetwork(wifiConfiguration);
            wifiManager.enableNetwork(wifiConfiguration.networkId, true);
            wifiManager.reconnect();

        }

如果有人能帮我,那就太酷了。我花了很多时间在这上面,真的不明白这个问题

您可以尝试查看此代码,这是我们用于连接网络的代码:

    private void connectToNetwork(String password, ScanResult result, String capabilities) {
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + result.SSID + "\"";

    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.ENABLED;

    if (capabilities.contains("WPA2")) {
        wc.preSharedKey = "\"" + password + "\"";
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    }

    if (capabilities.contains("WPA")) {
        wc.preSharedKey = "\"" + password + "\"";
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    }

    if (capabilities.contains("WEP")) {
        wc.wepKeys[0] = password;
        wc.wepTxKeyIndex = 0;
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    }

    if (capabilities.contains("TKIP")) {
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    }

    if (capabilities.contains("CCMP")) {
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.PairwiseCipher.CCMP);
    }

    if (!hasWifiSecurity(capabilities)) {
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    }

    // wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);

    // // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

    int res = wifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res);
    boolean b = wifi.enableNetwork(res, true);
    Log.d("WifiPreference", "enableNetwork returned " + b);
    Log.d("", "Reassociate: " + wifi.reassociate());
    Log.d("", "ReConnect:   " + wifi.reconnect());
    wifi.saveConfiguration();

    WifiInfo connection = wifi.getConnectionInfo();
    if (connection != null) {
        if (connection.getSupplicantState().equals(SupplicantState.INACTIVE)) {
            wifi.removeNetwork(res);
            wifi.saveConfiguration();
            scanForWiFiNetworks();

            wifi.enableNetwork(wifi.getConnectionInfo().getNetworkId(), true);

        }
    }
    // scanForWiFiNetworks();

    // if (password.equalsIgnoreCase("")) {
    // setScanningEnabled(true);
    // }
    // Toast.makeText(con, "Connecting to network: " + connectionInfo, Toast.LENGTH_SHORT).show(getMessageComments(dialog.getTextID()));
}

我们需要使用与2.4G网络相同的SSID连接到5G网络,因此唯一的方法是设置BSSID。我刚刚使用以下代码成功连接到几个指定的BSSID网络,请尝试:

WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
config.BSSID = BSSID; // <--BSSID should be set without ->"<-  (-__-)b...

// remove this network from the configed(saved) network list
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs)
{
    if (null != existingConfig && existingConfig.SSID.toString().equals("\"" + SSID + "\""))
    {
        mWifiManager.removeNetwork(existingConfig.networkId);
    }
}
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

//...
//...

// get the networkid 
int wcgID = mWifiManager.addNetwork(config);

// enabled this network
boolean b =  mWifiManager.enableNetwork(wcgID, true);

你解决这个问题了吗?介意分享你的结论吗?这个应用程序可用吗?