android连接指定的wifi
❶ Android如何添加隐藏的无线wifi配置文件
1、打开 设定 页面,点击 Wi-Fi
2、点击 添加WLAN网络
3、手动输入路由器的SSID、无线密码等参数,点击 储存,保存无线配置文件
4、无线配置文件添加完成,无线信号显示已连接
注意:添加了无线配置文件,手机重启后(或手机从外面拿回家)会自动连接路由器的无线信号,不需要重新添加无线配置文件。
❷ android怎么自动连接以zd开头的wifi,知道密码,想用代码直接连接
public class WifiAutoConnectManager {
private static final String TAG = WifiAutoConnectManager.class.getSimpleName();
WifiManager wifiManager;
// 定义几种加密方式,一种是WEP,一种是WPA,还有没有密码的情况 public enum WifiCipherType { WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID }
// 构造函数 public WifiAutoConnectManager(WifiManager wifiManager) { this.wifiManager = wifiManager; }
// 提供一个外部接口,传入要连接的无线网 public void connect(String ssid, String password, WifiCipherType type) { Thread thread = new Thread(new ConnectRunnable(ssid, password, type)); thread.start(); }
// 查看以前是否也配置过这个网络 private WifiConfiguration isExsits(String SSID) { List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } return null; }
private WifiConfiguration createWifiInfo(String SSID, String Password, WifiCipherType Type) { WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; // nopass if (Type == WifiCipherType.WIFICIPHER_NOPASS) { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } // wep if (Type == WifiCipherType.WIFICIPHER_WEP) { if (!TextUtils.isEmpty(Password)) { if (isHexWepKey(Password)) { config.wepKeys[0] = Password; } else { config.wepKeys[0] = "\"" + Password + "\""; } } config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); config.allowedKeyManagement.set(KeyMgmt.NONE); config.wepTxKeyIndex = 0; } // wpa if (Type == WifiCipherType.WIFICIPHER_WPA) { config.preSharedKey = "\"" + Password + "\""; 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.allowedProtocols.set(WifiConfiguration.Protocol.WPA); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; } return config; }
// 打开wifi功能 private boolean openWifi() { boolean bRet = true; if (!wifiManager.isWifiEnabled()) { bRet = wifiManager.setWifiEnabled(true); } return bRet; }
class ConnectRunnable implements Runnable { private String ssid;
private String password;
private WifiCipherType type;
public ConnectRunnable(String ssid, String password, WifiCipherType type) { this.ssid = ssid; this.password = password; this.type = type; }
@Override public void run() { // 打开wifi openWifi(); // 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi // 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句 while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { try { // 为了避免程序一直while循环,让它睡个100毫秒检测…… Thread.sleep(100); } catch (InterruptedException ie) { } }
WifiConfiguration wifiConfig = createWifiInfo(ssid, password, type); // if (wifiConfig == null) { Log.d(TAG, "wifiConfig is null!"); return; }
WifiConfiguration tempConfig = isExsits(ssid);
if (tempConfig != null) { wifiManager.removeNetwork(tempConfig.networkId); }
int netID = wifiManager.addNetwork(wifiConfig); boolean enabled = wifiManager.enableNetwork(netID, true); Log.d(TAG, "enableNetwork status enable=" + enabled); boolean connected = wifiManager.reconnect(); Log.d(TAG, "enableNetwork connected=" + connected); } }
private static boolean isHexWepKey(String wepKey) { final int len = wepKey.length();
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?) if (len != 10 && len != 26 && len != 58) { return false; }
return isHex(wepKey); }
private static boolean isHex(String key) { for (int i = key.length() - 1; i >= 0; i--) { final char c = key.charAt(i); if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) { return false; } }
return true; }}
❸ Android手机如何设置wifi设备名
1、首先打开手机设置,点击选择“其他无线连接”按钮。
❹ android wifi 开发中 如何判断成功连接上了指定的wifi热点
首先通过WifiManager对象获取系统服务的WIFI_SERVICE,然后调用getConnectionInfo()获得WifiInfo对象,再通过WifiInfo对象获取已连上wifi的SSID(实际上是housyunhin说的WifiInfo.getSSID()和其他wifi信息),之后判断是否匹配即可
❺ android 命令怎么连接wifi点
1234567891011121314151617/** * 检测网络是否连接 * @return */ private boolean checkNetworkState() { boolean flag = false; //得到网络连接信息 manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); //去进行判断网络是否连接 if (manager.getActiveNetworkInfo() != null) { flag = manager.getActiveNetworkInfo().isAvailable(); } if (!flag) { setNetwork(); } else { isNetworkAvailable(); }12 return flag; } /** * 网络未连接时,调用设置方法 */ private void setNetwork(){ Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("网络提示信息"); builder.setMessage("网络不可用,如果继续,请先设置网络!"); builder.setPositiveButton("设置", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = null; /** * 判断手机系统的版本!如果<a href="https:///s?wd=API&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">API</a>大于10 就是3.0+ * 因为3.0以上的版本的设置和3.0以下的设置不一样,调用的方法不同 */ if (android.os.Build.VERSION.SDK_INT > 10) { intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); } else { intent = new Intent(); ComponentName component = new ComponentName( "com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(component); intent.setAction("android.intent.action.VIEW"); } startActivity(intent); } });123 builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {3233343536 } }); builder.create(); builder.show(); } /** * 网络已经连接,然后去判断是wifi连接还是<a href="https:///s?wd=GPRS&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">GPRS</a>连接 * 设置一些自己的逻辑调用 */ private void isNetworkAvailable(){ State <a href="https:///s?wd=gprs&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">gprs</a> = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if(<a href="https:///s?wd=gprs&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">gprs</a> == State.CONNECTED gprs == State.CONNECTING){ Toast.makeText(this, "wifi is open! gprs", Toast.LENGTH_SHORT).show(); } //判断为wifi状态下才加载广告,如果是GPRS手机网络则不加载! if(wifi == State.CONNECTED wifi == State.CONNECTING){ Toast.makeText(this, "wifi is open! wifi", Toast.LENGTH_SHORT).show(); loadAdmob(); } } /** * 在wifi状态下 加载admob广告 */ private void loadAdmob(){ ll = (LinearLayout) findViewById(R.id.load_ads); ll.removeAllViews(); adsView = new AdView(this, AdSize.BANNER, "a15194a1ac9505d"); ll.addView(adsView); adsView.loadAd(new AdRequest()); }