Android连接WIFI

最新需要用到Android中连接WIFI的操作,特意去网上查看了下普遍的代码,但是对于WIFI的了解还是不足的,写下这个笔记帮助自个儿理解哈,我们用笔记本本扫描WIFI的时候,会出现一个列表,比如说我搜到的列表如下:

Android连接WIFI_第1张图片

此时,我们可以查看test_wifi的属性

Android连接WIFI_第2张图片

可以看到有这么两个属性:安全类型、加密类型、网络安全密钥。安全类型的下拉列表

Android连接WIFI_第3张图片

至于他们各自的区别我是不懂的还,我们可以利用WifiManager类下的startScan()方法来扫描周围wifi信号,扫描完毕之后用getScanResult()来获取扫描的结果,我们可以用一个ListView来显示结果,此时,我们可以通过

System.out.println(wifiList.get(i).SSID+wifiList.get(i).capabilities);

来输出各自的capbilities属性,得到的结果如下

Android连接WIFI_第4张图片

我们就可以根据capbilities的内容来配置我们需要连接的wifi信号

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		scanRet = wifiList.get(position);
		wifiConfigurations = wifiManager.getConfiguredNetworks();
		final EditText et = new EditText(this);
		et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		new AlertDialog.Builder(this).setTitle("请输入密码")
			.setMessage(scanRet.SSID)
			.setView(et)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
							
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					wc.SSID = "\"" + scanRet.SSID + "\""; // 配置wifi的SSID,即该热点的名称,如:TP-link_xxx
					wc.preSharedKey = "\"" + et.getText().toString() + "\""; // 该热点的密码
					Log.i("SSID", wc.SSID);
					Log.i("password", wc.preSharedKey);
					wc.hiddenSSID = true;
					wc.allowedAuthAlgorithms.clear();
					wc.allowedGroupCiphers.clear();
			        wc.allowedKeyManagement.clear();
			        wc.allowedPairwiseCiphers.clear();
			        wc.allowedProtocols.clear();
					if(scanRet.capabilities.contains("WPA") && !scanRet.capabilities.contains("WPA2"))
					{
						wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
						wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
						wc.status = WifiConfiguration.Status.ENABLED;
					}
					if(scanRet.capabilities.contains("WPA2"))
					{
						wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
						wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
						wc.status = WifiConfiguration.Status.ENABLED;
					}
					if(scanRet.capabilities.contains("PSK"))
					{
						wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
					}
					if(scanRet.capabilities.contains("TKIP"))
					{
						wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
						wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
					}
					if(scanRet.capabilities.contains("CCMP"))
					{
						wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
						wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
					}
					if(scanRet.capabilities.contains("EAP"))
					{
						wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP);
						wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
					}
					if(scanRet.capabilities.contains("WEP"))
					{
						wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
			        	wc.wepKeys[0] = "\"" + et.getText().toString() + "\"";
			        	wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			        	wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
			        	wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			        	wc.wepTxKeyIndex = 0;
					}
					if(scanRet.capabilities.equals(""))
					{
						wc.wepKeys[0] = "";
			        	wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			        	wc.wepTxKeyIndex = 0;
					}
					
					WifiConfiguration conf = IsExists(wc.SSID);
					if(conf != null)
					{
						wifiManager.removeNetwork(conf.networkId);
					}
		
					int temp = wifiManager.addNetwork(wc);
					if(wifiManager.enableNetwork(temp, true))
					{//连接成功
						Toast.makeText(WifiScanResultList.this, "连接成功", Toast.LENGTH_SHORT).show();
						setResult(RESULT_OK);
						WifiScanResultList.this.finish();
					}
					else
					{
						Toast.makeText(WifiScanResultList.this, "连接失败", Toast.LENGTH_SHORT).show();
					}
				}
			})
			.setNegativeButton("取消", null).show();
	}
	private WifiConfiguration IsExists(String SSID)
	{
		for(WifiConfiguration existingConfig : wifiConfigurations)
		{
			if(existingConfig.SSID.equals("\"" + SSID + "\""))
			{
				return existingConfig;
			}
		}
		return null;
	}

欢迎拍砖,大家共同进步。。。

你可能感兴趣的:(Android连接WIFI)