Creating WPA2 PSK Access Point in Android Programmatically

后端 未结 4 1313
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 16:09

I want to create Access Point in android programmatically with the following configurations. AccessPointName :SomeName Security:WPA2 PSK Password

4条回答
  •  野性不改
    2021-02-03 16:56

    I had faced this problem once. In order to create a WPA2 PSK access point, you need to populate the WifiConfiguartion object with your WPA2 PSK parameters. However I could not find a way to set KeyManagement as WPA2_PSK. There was only options for WPA_PSK, IEEE8021X, WPA_EAP and NONE. Then I read the android source code for WifiConfiguration.java. I was able to find out that indeed there is option for WPA2_PSK, but it is hidden by @hide, but it is an int with value 4. So what I did was to pass 4 in wifiConfiguration.allowedKeyManagement.set(4);. See code below.

    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifiConfiguration.SSID = "SomeName";
    wifiConfiguration.preSharedKey = "SomeKey";
    wifiConfiguration.hiddenSSID = false;
    wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wifiConfiguration.allowedKeyManagement.set(4);
    wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    

    And finally pass this wifiConfiguration using accesspoint as follows

    WifiApControl apControl = WifiApControl.getInstance(context);
    
    apControl.setEnabled(wifiConfiguration, true);
    

    or else you can use this wifiConfiguration with reflection techniques in java to activate the access point.

提交回复
热议问题