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

问题描述 投票:0回答:3

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

但我有一个问题:与特定 BSSID 的连接对于安全网络来说效果很好,但对于开放网络却不起作用,我不知道为什么。事实上,这种与具有特定 BSSID 的开放网络的连接适用于 Samsung 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();

        }

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

android connection android-wifi wifi bssid
3个回答
2
投票

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

    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
投票

我们需要连接到与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);

0
投票

它可以在最新版本的 Android 上运行吗???

© www.soinside.com 2019 - 2024. All rights reserved.