Creating WPA2 PSK Access Point in Android Programmatically

后端 未结 4 1314
爱一瞬间的悲伤
爱一瞬间的悲伤 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:49

    I was also facing same problem by passing 4 as KeyMangement solved my problem.

    WifiConfiguration myConfig = new WifiConfiguration();
            myConfig.SSID = ssid; // SSID name of netwok
            myConfig.preSharedKey = password; // password for network
            myConfig.allowedKeyManagement.set(4); // 4 is for KeyMgmt.WPA2_PSK which is not exposed by android KeyMgmt class
            myConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); // Set Auth Algorithms to open
            try {
                Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                return (Boolean) method.invoke(mWifiManager, myConfig, true);  // setting and turing on android wifiap with new configrations
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    Read more at Link

    0 讨论(0)
  • 2021-02-03 16:54

    You can use accesspoint:

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

    You just need to populate the WifiConfiguartion object with your WPA2 PSK parameters.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-03 17:04

    There is a SO Thread at How to create access point programmatically.

    However, if you see the code in that link, it is using JAVA Reflection technique to access underlying Android framework API. This is perhaps an indication that there is no direct API to toggle or program these system settings.

    Also, looking at API Summary of Settings.System class in Android docs (BTW it is "final" class which gives hint that not too many things will be editable), there is no evidence that Android offers any API for apps to toggle/program system settings.

    I gave the reference to that link only to highlight the fact that, to tweak Android system settings Such as APN, there seems to be no public/direct API provided by Android Framework, mostly because of Security Reasons.

    We had a requirement in our project where I needed to programatically toggle internet radio, and in another case, I needed to dismiss Telephony dialog.

    There is no public API to do that and only way to obtain desired result is using Reflection technique which is neither recommended nor portable and should be avoided.

    0 讨论(0)
提交回复
热议问题