gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:19:20 24 4
gpt4 key购买 nike

我正在创建一个 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();

}

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

最佳答案

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

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

关于android - 如何使用特定的 bssid android 连接到 wifi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32120710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com