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

标签 java android kotlin android-wifi

我正在创建一个 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/

相关文章:

java - String ... Java 中的键

java - apk 文件调试 - React Native

android - Gradle:如何定义多种口味的共同依赖?

android - 如何测试 Kotlin Coroutine actors

android - Observable 在 Kotlin 中列出

java - 发生异常: android. os.NetworkOnMainThreadException

java - 如何从文件读取并添加到对象数据?

java - Android Value ListView Kotlin

java - 将 JCombobox 渲染到正确的问题

Android - 如何使用 onKeyDown?