您的位置:首页 > 移动开发 > Android开发

android中连接到指定wifi

2016-02-18 17:21 525 查看
((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);
}
});

配置文件中需要的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: