gpt4 book ai didi

java - 以编程方式连接到 Android 中的隐藏 Wi-Fi 网络?

转载 作者:行者123 更新时间:2023-12-03 23:15:11 25 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,它应该连接到已知可用的隐藏 Wi-Fi 网络。

处理这种情况的正确方法是什么?

我已经尝试连接到一个隐藏的 wifi 网络。我尝试在操作系统版本为 6.0、7.0、7.1.1、8.0 的 Android 设备上尝试,但无法成功。

fun initiateWifiConnectivity(mContext: Context, sSID: String, password: String) {
mWifiManager = mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

if (!mWifiManager!!.isWifiEnabled) {
mWifiManager!!.isWifiEnabled = true
}

mWifiConfiguration = WifiConfiguration()
mWifiConfiguration!!.SSID = convertToQuotedString(sSID)
mWifiConfiguration!!.preSharedKey = password
mWifiConfiguration!!.status = WifiConfiguration.Status.ENABLED
mWifiConfiguration!!.hiddenSSID = true

mWifiConfiguration!!.allowedAuthAlgorithms.
set(WifiConfiguration.AuthAlgorithm.LEAP)

mWifiConfiguration!!.allowedGroupCiphers.
set(WifiConfiguration.GroupCipher.TKIP)

mWifiConfiguration!!.allowedGroupCiphers.
set(WifiConfiguration.GroupCipher.CCMP)

mWifiConfiguration!!.allowedGroupCiphers.
set(WifiConfiguration.GroupCipher.WEP40)

mWifiConfiguration!!.allowedKeyManagement.
set(WifiConfiguration.KeyMgmt.WPA_PSK)

mWifiConfiguration!!.allowedKeyManagement.
set(WifiConfiguration.KeyMgmt.WPA_EAP)

mWifiConfiguration!!.allowedKeyManagement.
set(WifiConfiguration.KeyMgmt.IEEE8021X)

mWifiConfiguration!!.allowedPairwiseCiphers.
set(WifiConfiguration.PairwiseCipher.TKIP)

mWifiConfiguration!!.allowedPairwiseCiphers.
set(WifiConfiguration.PairwiseCipher.CCMP)

mWifiConfiguration!!.allowedPairwiseCiphers.
set(WifiConfiguration.PairwiseCipher.NONE)

mWifiConfiguration!!.allowedProtocols.
set(WifiConfiguration.Protocol.RSN)

mWifiConfiguration!!.allowedProtocols.
set(WifiConfiguration.Protocol.WPA)

mWifiManager!!.addNetwork(mWifiConfiguration!!)

Handler().postDelayed(Runnable {
val list = mWifiManager!!.configuredNetworks
for (i in list) {
if (i.SSID != null && i.SSID ==
convertToQuotedString(sSID)) {

mWifiManager!!.disconnect()
mWifiManager!!.enableNetwork(i.networkId, true)
mWifiManager!!.reconnect()

break
}
}
}, 15000)
}

最佳答案

我在 Android Studio 中连接了一个隐藏的 WIFI 网络和一个 Android 7.0 设备。把 conf.hiddenSSID = true;对象 WifiConfiguration,连接网络的配置类似于一个显着的网络。

public class ShowActivity extends AppCompatActivity {

private WifiManager wifiManager; // Here is defined the instance

WifiConfiguration conf = new WifiConfiguration();
Log.d("Aut", Net + " : " + Pw);
conf.SSID = "\"" + Net + "\"";
conf.preSharedKey = "\"" + Pw + "\"";
conf.hiddenSSID = true; // Put this line to hidden SSID
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

// Connect Network

this.wifiManager =(WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
assert wifiManager != null;

int netId = this.wifiManager.addNetwork(conf);
WifiInfo wifi_inf = this.wifiManager.getConnectionInfo();
this.wifiManager.disableNetwork(wifi_inf.getNetworkId());
this.wifiManager.enableNetwork(netId, true);
this.wifiManager.reconnect();
}

关于java - 以编程方式连接到 Android 中的隐藏 Wi-Fi 网络?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56230831/

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