Search code examples
androidandroid-wifiwifi

How do I create an open wifiap?


I have created an wifiap with a password, but for a project I would like to create a open network. I have tried the following, but if there already is a password, then it is still active.

        Method getWifiConfig = mWifiManager.getClass().getMethod("getWifiApConfiguration",null);
        WifiConfiguration wifiConf  = (WifiConfiguration)getWifiConfig.invoke(mWifiManager, null); 
        wifiConf.SSID = "enny";
        //wifiConf.preSharedKey = "fuunnv12345";

        wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        Method setWifiConfig = mWifiManager.getClass().getMethod("setWifiApConfiguration",WifiConfiguration.class);
        setWifiConfig.invoke(mWifiManager,wifiConf);
        Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method.invoke(mWifiManager, null, enabled);

Solution

  • I found a solution. Inspired by this answer, How to programmatically create and read WEP/EAP WiFi configurations in Android?, I got information from the wificonfiguration in the wifiAP. This showed which fields were set to true when configuring a wifiAP by using the normal setup provided in Android. So when I want to change a wifiAP from a locked to an open I set these keys to false. For my case I set the following parameters:

    wifiConf.preSharedKey = null;
    wifiConf.allowedKeyManagement.set(KeyMgmt.NONE); 
    wifiConf.allowedKeyManagement.set(KeyMgmt.WPA2_PSK,false);
    wifiConf.allowedKeyManagement.set(KeyMgmt.WPA_PSK,false);