android Wifi自动连接

通过程序来实现wifi的自动连接。

       这两天对android的wifi功能研究了一下。下面共享出自己封装的WifiConnect类。(这里参考了ZXing开源项目中wifi模块)

       首先,要了解android关于wifi的API.

       如果你喜欢看English API,这是android关于wifi的API地址:

http://developer.android.com/reference/android/net/wifi/package-summary.html(配置Wifi,还需要设定权限,android中关于wifi的API文档中说明了要添加什么权限)

       如果你一看英语就头疼,这里有关于wifi的api的中文解释

http://note.sdo.com/u/1500295617/n/prb71~jGAHfpnM0l000bgN

       切入正题,这里贴出一个封装了的类(WifiConnect),在实例化WifiConnect对象时,需要传入一个WifiManager对象。WifiConnect类向外部提供一个Connect方法,参数是无线的SSID,password,还有加密类型。

/*
 *  WifiConnect.java
 *  Author: cscmaker
 */
package com.wifi.connect;

import java.util.List;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

public class WifiConnect {

    WifiManager wifiManager;
    
//定义几种加密方式,一种是WEP,一种是WPA,还有没有密码的情况
    public enum WifiCipherType
    {
  	  WIFICIPHER_WEP,WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
    }
	
//构造函数
	public WifiConnect(WifiManager wifiManager)
	{
	  this.wifiManager = wifiManager;
	}
	
//打开wifi功能
     private boolean OpenWifi()
     {
    	 boolean bRet = true;
         if (!wifiManager.isWifiEnabled())
         {
       	  bRet = wifiManager.setWifiEnabled(true);  
         }
         return bRet;
     }
    
//提供一个外部接口,传入要连接的无线网
     public boolean Connect(String SSID, String Password, WifiCipherType Type)
     {
        if(!this.OpenWifi())
    	{
    		 return false;
    	}
//开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
//状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
        while(wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING )
        {
        	 try{
     //为了避免程序一直while循环,让它睡个100毫秒在检测……
           	  Thread.currentThread();
			  Thread.sleep(100);
           	}
           	catch(InterruptedException ie){
           }
        }
       
    WifiConfiguration wifiConfig = this.CreateWifiInfo(SSID, Password, Type);
		//
    	if(wifiConfig == null)
		{
    	       return false;
		}
	   	
        WifiConfiguration tempConfig = this.IsExsits(SSID);
        
        if(tempConfig != null)
        {
        	wifiManager.removeNetwork(tempConfig.networkId);
        }
        
      int netID = wifiManager.addNetwork(wifiConfig);
    	boolean bRet = wifiManager.enableNetwork(netID, false);  
		return bRet;
     }
     
    //查看以前是否也配置过这个网络
     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 + "\"";  
     	if(Type == WifiCipherType.WIFICIPHER_NOPASS)
     	{
     		 config.wepKeys[0] = "";
     		 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
     		 config.wepTxKeyIndex = 0;
     	}
     	if(Type == WifiCipherType.WIFICIPHER_WEP)
     	{
     		config.preSharedKey = "\""+Password+"\""; 
     		config.hiddenSSID = true;  
     	    config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
     	    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
     	    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
     	    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
     	    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
     	    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
     	    config.wepTxKeyIndex = 0;
     	}
     	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.status = WifiConfiguration.Status.ENABLED;  
     	}
     	else
     	{
     		return null;
     	}
     	return config;
     }
     
}

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值