如何在Android中使用ssid和密码创建自定义WPA热点? [英] How to create Custom WPA Hotspot with ssid and password in android?
问题描述
我正在使用以下代码创建Wifi热点配置. 我能够创建Hotspot配置并启用它.但是我已经为WPA-PSK提供了配置,但是它始终被视为OPEN网络.
I am using the following code for creating the Wifi-hotspot configuration. I am able to create the Hotspot configuration and able to enable it. but i have give configuration for WPA-PSK, But it always taken as OPEN network.
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;
}
运行此应用程序后,热点已启用.请检查下面的图像.
After run this app the Hotspot got enabled. Please check the image below.
如何在android应用中设置WPA_PSK wifi配置?
How to set the WPA_PSK wifi configuration in android app?
按照下面的答案,我已经修改了下面的代码.
As per below answer i have modified the code below.
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, netConfig, true);
} catch (Exception e) {
CommonUtil.log(TAG, e.getMessage());
return false;
}
return apstatus;
}
推荐答案
我已经在我的应用程序中完成了此操作.请在下面检查我的代码.
I have done this in my app. Please check my code below.
首先在清单中添加以下权限.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
然后在MyHotspotManager .java类下创建
public class MyHotspotManager {
private WifiConfiguration wifiCon;
private HotspotDetails hotspotDetails;
private WifiManager mWifiManager;
public MyHotspotManager(WifiManager wifiManager){
this.mWifiManager = wifiManager;
}
public boolean setHotspot(HotspotDetails hotspotDetails) {
this.hotspotDetails = hotspotDetails;
boolean apstatus;
wifiCon = new WifiConfiguration();
wifiCon.SSID = hotspotDetails.getSsid();
wifiCon.preSharedKey = hotspotDetails.getPassword();
wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiCon.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, wifiCon, true);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
return apstatus;
}
}
之后,您可以在活动中的任何位置调用setHotspot方法,如下所示.在这里,我必须检查应用程序是否具有ACTION_MANAGE_WRITE_SETTINGS权限.
public void OnButtonClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(getApplicationContext())) {
Toast.makeText(this, "Please try again after giving access to Change system settings", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 200);
}else{
createHotspot();
}
}
}
private void createHotspot(){
HotspotDetails hotspotDetails = new HotspotDetails();
hotspotDetails.setSsid("TestStackOverFlow");
hotspotDetails.setPassword("654321");
new MyHotspotManager((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE))
.setHotspot(hotspotDetails);
}
但是,如果需要,可以通过将build.gradle中的targetSdkVersion更改为21来省略权限请求部分.现在,您可以按以下方式调用它
public void OnButtonClick(View view) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// if (!Settings.System.canWrite(getApplicationContext())) {
// Toast.makeText(this, "Please try again after giving access to Change system settings", Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
// startActivityForResult(intent, 200);
//
// }else{
// }
// }
createHotspot();
}
private void createHotspot(){
HotspotDetails hotspotDetails = new HotspotDetails();
hotspotDetails.setSsid("TestStackOverFlow");
hotspotDetails.setPassword("654321");
new MyHotspotManager((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE))
.setHotspot(hotspotDetails);
}
这是我的HotspotDetails.java类
public class HotspotDetails {
private String ssid;
private String password;
public String getSsid() {
return ssid;
}
public void setSsid(String ssid) {
this.ssid = ssid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return ssid + ":" + password;
}
}
这篇关于如何在Android中使用ssid和密码创建自定义WPA热点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!