android中连接到指定wifi

((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
			
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//连接指定wifi
		WifiManager manager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE); 
		WifiConfiguration config = new WifiConfiguration();
		config.SSID = "\"wifi名\"";
		config.preSharedKey = null;//非加密wifi
//		config.preSharedKey = "\"wifi密码\"";//加密wifi
		config.hiddenSSID = true;
		config.status = WifiConfiguration.Status.ENABLED;
		config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
		config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
		config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
		config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
		config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);//WPA_PSK  NONE(非加密)
		config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);   
		int netId = manager.addNetwork(config);
		boolean b = manager.enableNetwork(netId, true);
	}
});

配置文件中需要的权限:




你可能感兴趣的:(Android,Android,系统功能相关)