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

3

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

在处理这种情况时,哪种方法是正确的?

我已经尝试连接到隐藏的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)
}

1
https://dev59.com/fI7da4cB1Zd3GeqP7QC0#36405150 - Nikunj Paradva
@NikunjParadva 我已经尝试过了,但是没有连接成功。我会再检查一下。 - Ahamed Mujeeb
1
@AhamedMujeeb,你找到解决方案了吗?能否分享一下? - Anant Shah
@AnantShah 目前还没有解决方案。需求仍然存在。为了继续工作,我们暂时将 WiFi 从硬件层面变成可见的 WiFi。 - Ahamed Mujeeb
1个回答

1
我在Android Studio中连接了一个隐藏的WIFI网络,使用的是Android 7.0版本的设备。通过设置WifiConfiguration对象的conf.hiddenSSID = true;属性,连接网络的配置与普通网络类似。
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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接