【问题标题】:Connecting to a Hidden Wi-Fi network in Android programmatically?以编程方式连接到 Android 中的隐藏 Wi-Fi 网络?
【发布时间】:2019-10-07 09:47:26
【问题描述】:

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

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

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

【问题讨论】:

  • @NikunjParadva 我已经尝试过了,但没有连接。会检查
  • @AhamedMujeeb 你找到解决方案了吗?可以分享一下吗?
  • @AnantShah 尚无解决方案。存在需求。暂时使 wifi 成为硬件级别的可见 wifi 以继续工作。

标签: java android kotlin android-wifi


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多