public class MainActivity extends Activity {
WifiManager mWifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Button button = (Button) findViewById(R.id.button);
final EditText edit_ssid = (EditText) findViewById(R.id.edit1);
final EditText edit_psw = (EditText) findViewById(R.id.edit2);
final EditText edit_type = (EditText) findViewById(R.id.edit3);
edit_ssid.setText("iDataOffice");
edit_psw.setText("IntegralTek");
edit_type.setText(""+3);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WifiConfiguration wificonfig = CreateWifiInfo(edit_ssid.getText().toString().trim(),
edit_psw.getText().toString().trim(),
Integer.parseInt(edit_type.getText().toString()));
int id = mWifiManager.addNetwork(wificonfig);
mWifiManager.saveConfiguration();
mWifiManager.enableNetwork(id, true);
}
});
}
public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type)
{
WifiConfiguration config = this.IsExsits(SSID);
if(config != null) {
mWifiManager.removeNetwork(config.networkId);
}
config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
if(Type == 1) //WIFICIPHER_NOPASS
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 2) //WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0]= "\""+Password+"\"";
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 3) //WIFICIPHER_WPA
{
config.preSharedKey = "\""+Password+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
private WifiConfiguration IsExsits(String sSID) {
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
if(null == existingConfigs || existingConfigs.equals("")){
return null;
}
for(WifiConfiguration existingConfig : existingConfigs){
if(existingConfig.SSID.toString().equals("\"" + sSID + "\"")){
return existingConfig;
}
}
return null;
}
}
WifiManager mWifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Button button = (Button) findViewById(R.id.button);
final EditText edit_ssid = (EditText) findViewById(R.id.edit1);
final EditText edit_psw = (EditText) findViewById(R.id.edit2);
final EditText edit_type = (EditText) findViewById(R.id.edit3);
edit_ssid.setText("iDataOffice");
edit_psw.setText("IntegralTek");
edit_type.setText(""+3);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WifiConfiguration wificonfig = CreateWifiInfo(edit_ssid.getText().toString().trim(),
edit_psw.getText().toString().trim(),
Integer.parseInt(edit_type.getText().toString()));
int id = mWifiManager.addNetwork(wificonfig);
mWifiManager.saveConfiguration();
mWifiManager.enableNetwork(id, true);
}
});
}
public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type)
{
WifiConfiguration config = this.IsExsits(SSID);
if(config != null) {
mWifiManager.removeNetwork(config.networkId);
}
config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
if(Type == 1) //WIFICIPHER_NOPASS
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 2) //WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0]= "\""+Password+"\"";
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 3) //WIFICIPHER_WPA
{
config.preSharedKey = "\""+Password+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
private WifiConfiguration IsExsits(String sSID) {
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
if(null == existingConfigs || existingConfigs.equals("")){
return null;
}
for(WifiConfiguration existingConfig : existingConfigs){
if(existingConfig.SSID.toString().equals("\"" + sSID + "\"")){
return existingConfig;
}
}
return null;
}
}