WiFi无法连接

时间:2018-12-11 10:25:54

标签: android android-wifi

我一直在尝试以编程方式连接到wifi网络。如果我通过设置将其连接,然后尝试以编程方式进行连接,则它将起作用。否则它将无法正常工作。它正在连接,并在1秒钟内断开连接。

 public void saveWpaConfig(String SSID, String Password)
{
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration conf = new WifiConfiguration();
    conf.hiddenSSID = true;
    conf.priority = 1000;
    conf.SSID = "\"" + SSID + "\"";
    conf.preSharedKey = "\"" + Password + "\"";
    conf.status = WifiConfiguration.Status.ENABLED;
    conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

    conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    int res = wifiManager.addNetwork(conf);
    boolean es = wifiManager.saveConfiguration();
    Log.d("", "saveConfiguration returned " + es);
    wifiManager.disconnect();
    boolean bRet = wifiManager.enableNetwork(res, true);
    Log.i("", "enableNetwork bRet = " + bRet);
    wifiManager.reconnect();
}

0 个答案:

没有答案