android 连接指定wifi

批量新机器连接wifi,写了apk来连接
首先是获取wifimanager

  mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

然后连接

    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();

    // 指定对应的SSID
    config.SSID = "\"" + "ssid" + "\"";

    config.preSharedKey = "\"" + "pwd" + "\"";
    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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    config.status = WifiConfiguration.Status.ENABLED;

    int netId = mWifiManager.addNetwork(config);
    Log.e("TAG", netId + "   ");
    // 这个方法的第一个参数是需要连接wifi网络的networkId,第二个参数是指连接当前wifi网络是否需要断开其他网络
    // 无论是否连接上,都返回true。。。。
    mWifiManager.enableNetwork(netId, true);

需要的权限是










你可能感兴趣的:(笔记备忘)