WIFICONFIGURATION.java类
public class WiFiConfiguration extends Activity {
Button btnnext;
public String networkSSID = null;
public String networkPass = null;
public Button ConnectButton;
private BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_wifi_configuration);
btnnext = (Button) findViewById(R.id.btnnext);
ConnectButton = (Button) findViewById(R.id.ApartmentButton);
ConnectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
networkSSID = "helloworld";
networkPass = "mypass";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
/* for wep*/
//conf.wepKeys[0] = "\"" + networkPass + "\"";
//conf.wepTxKeyIndex = 0;
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
/* for wpa*/
conf.preSharedKey = "\"" + networkPass + "\"";
/* for open network*/
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Context context = getApplicationContext();
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(wifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
wifiManager.setWifiEnabled(true);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
Log.v("rht", "WifiConfiguration SSID " + i.SSID);
boolean isDisconnected = wifiManager.disconnect();
Log.v("rht", "isDisconnected : " + isDisconnected);
boolean isEnabled = wifiManager.enableNetwork(i.networkId, true);
Log.v("rht", "isEnabled : " + isEnabled);
boolean isReconnected = wifiManager.reconnect();
Log.v("rht", "isReconnected : " + isReconnected);
Toast.makeText(context, "Connection started", 400).show();
/*if (wifiManager.getConnectionInfo()==){
Toast.makeText(context, "Connection success", 400).show();
}
else {
Toast.makeText(context, "Connection fail", 400).show();*/
}
break;
}
}
});
}
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
this.registerReceiver(mWifiStateChangeReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(mWifiStateChangeReceiver);
}
private BroadcastReceiver mWifiStateChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
if (action.equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
// just to be more sure, you can also check whether the connected APN as same as the required one
// by comparing APN names
if (networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
Toast.makeText(context, "Connection success", 400).show();
/*Intent i = new Intent(context, secondActivity.class);
startActivity(i);*/
}
else
{
Toast.makeText(context, "Connection fali", 400).show();
}
}
}
}
}
}
}
};
} }