我只能连接一次wifi吗?

时间:2019-05-31 07:22:03

标签: java android wifi wifimanager

我通过编程方式在我的应用中以这种方式连接wifi

private void WPA(String networkSSID, String networkPass, WifiManager wifiManager, String command) {
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + networkSSID + "\"";
    wc.preSharedKey = "\"" + networkPass + "\"";
    wc.status = WifiConfiguration.Status.ENABLED;
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    if (command.equals(ADD)) {
        addNetwork(wifiManager, wc);
    } else if (command.equals(REMOVE)) {
        removeNetwork(wifiManager, wc);
    }

}

private void addNetwork(WifiManager wifiManager, WifiConfiguration wc) {
    if (!wifiManager.isWifiEnabled()) enableWifi();
    int id = wifiManager.addNetwork(wc);
    if (id > -1) {
        wifiManager.disconnect();
        wifiManager.enableNetwork(id, true);
        wifiManager.reconnect();
        wifiManager.saveConfiguration();
    }
}

我想知道在Android中是否可以不保存密码而连接wifi,当wifi断开连接时,用户如果不输入密码就无法再次连接?如果有可能,我该如何以编程方式做到这一点?

感谢进阶!

1 个答案:

答案 0 :(得分:0)

看看这篇帖子how to extract wifi pass

,然后在程序中对其进行修改,以删除所需的配置并将其保存回去。