如何在android中使用ssid和密码创建自定义WPA热点?

前端之家收集整理的这篇文章主要介绍了如何在android中使用ssid和密码创建自定义WPA热点?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码创建Wifi-hotspot配置.
我能够创建Hotspot配置并能够启用它.但我已经为WPA-PSK提供配置,但它总是作为OPEN网络.
public boolean setHotSpot(String SSID,String passWord){
Method[] mMethods = mWifiManager.getClass().getDeclaredMethods();

    for(Method mMethod: mMethods){

        if(mMethod.getName().equals("setWifiApEnabled")) {
            WifiConfiguration netConfig = new WifiConfiguration();
             netConfig.SSID = SSID ;
                netConfig.preSharedKey = passWord;
                netConfig.hiddenSSID = true;
                netConfig.status = WifiConfiguration.Status.ENABLED;
                netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                netConfig.allowedKeyManagement.set(4);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

            try {              
                mMethod.invoke(mWifiManager,netConfig,true);
                mWifiManager.saveConfiguration();
                return true;

            } catch (Exception e) {
                e.printStackTrace();
                CommonUtil.log(TAG,"Exception : "+e.getLocalizedMessage());
            }
        }
    }
return false;

}

运行此应用程序后,Hotspot已启用.请查看下面的图片.

如何在Android应用程序中设置WPA_PSK wifi配置?

根据以下答案,我修改了下面的代码.

public boolean setHotSpot(String SSID,String passWord) {
    boolean apstatus;
    WifiConfiguration netConfig = new WifiConfiguration();
    if (passWord == "") {
        netConfig.SSID = SSID;
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    } else {
        netConfig.SSID = SSID;
        netConfig.preSharedKey = passWord;
                /*netConfig.hiddenSSID = true;
                netConfig.status = WifiConfiguration.Status.ENABLED;
                netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                netConfig.allowedKeyManagement.set(4);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);*/
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    }

    try {
        Method setWifiApMethod = mWifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
        //setWifiApMethod.invoke(mWifiManager,prevIoUsConfigurations,false);
        //Thread.sleep(2000);
        apstatus = (Boolean) setWifiApMethod.invoke(mWifiManager,true);
    } catch (Exception e) {
        CommonUtil.log(TAG,e.getMessage());
        return false;
    }
    return apstatus;
}

解决方法

netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

将以上OPEN替换为SHARED

netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
原文链接:https://www.f2er.com/android/318284.html

猜你在找的Android相关文章