让Android连接指定wifi,需要添加如下权限

    

热点的加密方式不同,其接入的方式也不同,但均是通过WifiConfigration来配置的,分为以下三种情况:

1、完全开放的网络

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

2、WEP加密类型的网络

conf.wepKeys[0] = psd;                conf.wepTxKeyIndex = 0;                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

3、WPA加密类型的网络

conf.preSharedKey = psd;


明白原理后,写一个Activity测试,代码如下:

package test.wifi;import android.content.Context;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiManager;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.cxq.selftestdemo.R;import java.util.List;public class TestWifiActivity extends AppCompatActivity {    private static final String TAG = "TestWifiActivity";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test_wifi);    }    public void begin(View v) {    }    public void end(View v) {        connectWifi(networkSSID, networkPass, WifiEncType.WPA);    }    String networkSSID = "cxq.ssid";    String networkPass = "cxq.password";    enum WifiEncType {        WEP, WPA, OPEN    }    public void connectWifi(String targetSsid, String targetPsd, WifiEncType enc) {        // 1、注意热点和密码均包含引号,此处需要需要转义引号        String ssid = "\"" + targetSsid + "\"";        String psd = "\"" + targetPsd + "\"";        //2、配置wifi信息        WifiConfiguration conf = new WifiConfiguration();        conf.SSID = ssid;        switch (enc) {            case WEP:                // 加密类型为WEP                conf.wepKeys[0] = psd;                conf.wepTxKeyIndex = 0;                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);                break;            case WPA:                // 加密类型为WPA                conf.preSharedKey = psd;                break;            case OPEN:                //开放网络                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);        }        //3、链接wifi        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);        wifiManager.addNetwork(conf);        List list = wifiManager.getConfiguredNetworks();        for (WifiConfiguration i : list) {            if (i.SSID != null && i.SSID.equals(ssid)) {                wifiManager.disconnect();                wifiManager.enableNetwork(i.networkId, true);                wifiManager.reconnect();                break;            }        }    }}


更多相关文章

  1. [Android] AsyncTask使用实例---加载网络图片
  2. 【Android】volley网络框架的文件下载
  3. android 判断网络是否断开
  4. android HttpClient网络通信工具类基于XML
  5. Android设置角标提示
  6. android 打开文件
  7. Android简单类型资源(Bool、Color、Dimension、ID、Integer、Inte
  8. Android(安卓)监控网络状态
  9. Android(安卓)连接加密网络

随机推荐

  1. Android网络状态相关
  2. Android实现View的任意拖动
  3. [zz]android框架示意图
  4. Mac通过Android aapt解析apk包名、版本号
  5. Volley的基本使用
  6. Android Jni开发环境搭建完整版
  7. EditText点击隐藏hint
  8. Android中Activity启动过程源码阅读笔记:
  9. 【Android】关联source code的方法
  10. Android监听耳机按键事件