赞
踩
批量新机器连接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);
需要的权限是
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 网络状态改变的权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!-- 6.0以上打开蓝牙和wifi最好加上定位权限,获取wifi列表要用 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 获取IP地址 -->
<uses-permission android:name="android.permission.INTERNET"/>
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。