我使用以下代码创建Wifi-hotspot配置.
我能够创建Hotspot配置并能够启用它.但我已经为WPA-PSK提供配置,但它总是作为OPEN网络.
我能够创建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);