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

时间:2019-05-21 03:51:15

标签: 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)
}

1 个答案:

答案 0 :(得分:0)

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