Android Wifi 操作封装类

Android WiFi 操作封装类

自己开发整理了一个Android下的WiFi管理操作类,有需要的人可以使用它。

1. WiFi操作封装类WifiAdmin  

该类中封装了WiFi的打开、关闭、扫描、连接、断开、监听WiFi及网络事件等等功能

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Parcelable;
import android.text.TextUtils;

/**
 * A class to operate WiFi such as open, close, scan, connect, disconnect, monitor network event, etc.
 * @author Kyven.zhang@126.com
 *
 */
public class WifiAdmin
{
	private Context context;
	private WifiManager wifiManager;
	private List<ScanResult> wifiList;
	
	public enum WifiCipherType
	{
		WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
	}

	public WifiAdmin(Context context)
	{
		this.context = context;
		if(this.context != null)
			wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	}

	/**
	 * Open WiFi and wait the result
	 * 
	 * @param waitSeconds specify 0 to return immediately; specify seconds with
	 *            integer greater than 0 to wait the WiFi to get ready.
	 * @return true if succeeded, false if failed.
	 */
	public boolean openWifi(int waitSeconds)
	{
		if (wifiManager == null)
			return false;
		if (!wifiManager.isWifiEnabled())
		{
			return wifiManager.setWifiEnabled(true);
		}
		if (waitSeconds <= 0)
			return true;

		/**
		 * It takes a few seconds to enable the WiFi, we detect the status intermittently until it's enabled or timeout.
		 */
		while ((wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) && (waitSeconds-- > 0))
		{
			try
			{
				Thread.sleep(300);
			} 
			catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
			return false;
		return true;
	}

	/**
	 * Close the WiFi
	 * @return true if succeeded, false if failed.
	 */
	public boolean closeWifi()
	{
		if (wifiManager == null)
			return false;
		if (wifiManager.isWifiEnabled())
		{
			return wifiManager.setWifiEnabled(false);
		}
		return true;
	}

	/**
	 * Check the WiFi state
	 * @return the WiFi state, it could be WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED, WIFI_STATE_ENABLING, or WIFI_STATE_UNKNOWN
	 */
	public int checkState()
	{
		if (wifiManager == null)
			return -1;
		return wifiManager.getWifiState();
	}

	/**
	 * Create a WiFi Lock
	 * <p>Wifi Lock allows an application to keep the Wi-Fi radio awake</p>
	 * @param lockName the WiFi Lock name.
	 * @return A WifiLock object
	 */
	public WifiLock createWifiLock(String lockName)
	{
		if (lockName == null || lockName == "")
			return null;
		return wifiManager.createWifiLock(lockName);
	}

	/**
	 * Enable the WifiLock
	 * @param wifiLock the WifiLock to enable.
	 */
	public void acquireWifiLock(WifiLock wifiLock)
	{
		if (wifiLock == null)
			return;
		wifiLock.acquire();
	}

	/**
	 * Disable the Wifi Lock
	 * @param wifiLock the WifiLock to disable.
	 */
	public void releaseWifiLock(WifiLock wifiLock)
	{
		if (wifiLock == null)
			return;
		//check if it's locked.
		if (wifiLock.isHeld())
		{
			wifiLock.release();
		}
	}

	/**
	 * Check if a WifiLock is enabled
	 * @param wifiLock
	 * @return true if it's locked, false if it's not locked.
	 */
	public boolean isLocked(WifiLock wifiLock)
	{
		if (wifiLock == null)
			return false;
		return wifiLock.isHeld();
	}

	/**
	 * Scan to get AP list.
	 */
	public void scan()
	{
		if (wifiManager != null)
		{
			wifiManager.startScan();
		}
	}

	/**
	 * Get all the WiFi access point with the same SSID. Must call scan once at
	 * least before calling this function
	 * 
	 * @return list of WiFi access point.
	 */
	public List<ScanResult> getWifiListBySSID()
	{
		if (wifiList != null)
			wifiList.clear();
		wifiList = wifiManager.getScanResults();
		if(wifiList == null)
			return null;
		/**
		 * because WifiManager.startScan return all the Access Points with
		 * different BSSID, but usually, we only care about the SSID when we
		 * connect to a wireless network, so we filter the BSSID list and only keep
		 * one Access Point for all the BSSIDs with the same SSID.
		 */
		boolean addedAlready = false;
		List<ScanResult> listScanResult = new ArrayList<ScanResult>();
		for (ScanResult scanResult : wifiList)
		{
			// check if this SSID has been added.
			addedAlready = false;
			for (ScanResult sr : listScanResult)
			{
				if (sr.SSID.equals(scanResult.SSID))
				{
					addedAlready = true;
					break;
				}
			}

			// if not added yet, add it.
			if (!addedAlready)
			{
				/**
				 * Here I want to get the BSSID with best signal level for every
				 * SSID。It's optional. ScanResult.level (also called RSSI) is
				 * always negative. the smaller value has worse signal. i.e. -64
				 * has worse signal than -61. In android framework, worst signal
				 * is defined by MIN_RSSI = -100, and best signal is defined by
				 * MAX_RSSI = -55.
				 */
				ScanResult bestSignalOne = findTheBestSignalBSSID(scanResult.SSID, wifiList);
				if (bestSignalOne != null)
				{
					listScanResult.add(bestSignalOne);
				}

				/**
				 * if you don't care about the signal strength, you can comment
				 * above code and just use the below one line.
				 */
				// listScanResult.add(scanResult);

			}
		}
		return listScanResult;
	}

	/**
	 * Get all the WiFi access point with different BSSID. Must call scan once
	 * at least before calling this function
	 * 
	 * @return list of WiFi access point.
	 */
	public List<ScanResult> getWifiListByBSSID()
	{
		if (wifiList != null)
			wifiList.clear();
		wifiList = wifiManager.getScanResults();
		return wifiList;
	}

	/**
	 * Get all the configured access points.
	 * 
	 * @return list of the configured WiFi configurations
	 */
	public List<WifiConfiguration> getConfiguredNetworks()
	{
		if (wifiManager == null)
		{
			return null;
		}
		return wifiManager.getConfiguredNetworks();
	}

	/**
	 * Connect the specified WiFi Access Point by automatically detecting the
	 * WiFi Cipher Type. Must call scan once at least before calling this
	 * function.
	 * 
	 * @param ssid the SSID of the WiFi access point
	 * @param password the connection password.
	 */
	public void connect(String ssid, String password)
	{
		if (wifiManager == null)
			return;
		List<ScanResult> listScanResult = getWifiListBySSID();
		if (listScanResult == null)
			return;

		WifiCipherType cipherType = WifiCipherType.WIFICIPHER_INVALID;
		for (ScanResult scanResult : listScanResult)
		{
			if (scanResult.SSID.equalsIgnoreCase(ssid))
			{
				if (scanResult.capabilities.contains("WEP"))
				{
					cipherType = WifiCipherType.WIFICIPHER_WEP;
				} 
				else if (scanResult.capabilities.contains("WPA"))
				{
					cipherType = WifiCipherType.WIFICIPHER_WPA;
				} 
				// if neither WEP nor WPA is specified, we assume it has no password required.
				else
				{
					cipherType = WifiCipherType.WIFICIPHER_NOPASS;
				}
				break;
			}
		}
		if (cipherType == WifiCipherType.WIFICIPHER_INVALID)
			return;

		// remove the existing configuration entry to avoid multiple same entry added in the system WiFi configuration list.
		WifiConfiguration existConfig = getExsitConfig(ssid);
		if (existConfig != null)
		{
			wifiManager.removeNetwork(existConfig.networkId);
		}

		WifiConfiguration wifiConfig = createWifiConfig(ssid, password,
				cipherType);
		if (wifiConfig == null)
			return;
		Thread thread = new Thread(new ConnectThread(wifiManager, wifiConfig, true));
		thread.start();

	}

	/**
	 * Connect the specified WiFi Access Point by specifying the WiFi Cipher
	 * Type.
	 * 
	 * @param ssid the SSID of the WiFi access point
	 * @param password the connection password
	 * @param cipherType the WiFi Cipher Type.
	 */
	public void connect(String ssid, String password, WifiCipherType cipherType)
	{
		if (wifiManager == null)
			return;

		// remove the existing configuration entry to avoid multiple same entry added in the system WiFi configuration list.
		WifiConfiguration existConfig = getExsitConfig(ssid);
		if (existConfig != null)
		{
			wifiManager.removeNetwork(existConfig.networkId);
		}

		WifiConfiguration wifiConfig = createWifiConfig(ssid, password,	cipherType);
		if (wifiConfig == null)
			return;
		Thread thread = new Thread(new ConnectThread(wifiManager, wifiConfig, true));
		thread.start();

	}

	/**
	 * Connect the existing configured WiFi Access Point.
	 * 
	 * @param ssid the SSID of the WiFi access point
	 * 
	 */
	public void connectConfiguredNetwork(String ssid)
	{
		List<WifiConfiguration> listWiFiConfig = getConfiguredNetworks();
		if (listWiFiConfig == null)
			return;
		String tempSSID = "\"" + ssid + "\"";
		WifiConfiguration existWiFiConfig;
		for (WifiConfiguration wifiConfig : listWiFiConfig)
		{
			if (wifiConfig.SSID.equalsIgnoreCase(tempSSID))
			{
				existWiFiConfig = wifiConfig;
				Thread thread = new Thread(new ConnectThread(wifiManager, existWiFiConfig, false));
				thread.start();
				break;
			}
		}

	}

	/**
	 * List the scan result
	 * @return A string containing the information of every AP scanned.
	 * 
	 */
	public StringBuffer listScanResult()
	{
		if(wifiList == null)
			return null;
		StringBuffer sb = new StringBuffer();
		int i=0;
		for(ScanResult sr : wifiList)
		{
			sb.append(Integer.toString(i++) + "\t"+ sr.toString() + "\n");
		}
		return sb;
	}

	/**
	 * Remove a WiFi configuration with name specified by ssid.
	 * @param ssid the AP name
	 */
	public void removeConfiguration(String ssid)
	{
		WifiConfiguration existConfig = getExsitConfig(ssid);

		if (existConfig != null)
		{
			wifiManager.removeNetwork(existConfig.networkId);
		}
	}

	/**
	 * Disconnect from the AP 
	 * @param ssid the AP name
	 */
	public void disconnect(String ssid)
	{
		if (wifiManager == null)
			return;
		List<WifiConfiguration> wifiConfigs = wifiManager.getConfiguredNetworks();
		if (wifiConfigs == null)
			return;
		int netID = 0;
		for (WifiConfiguration wifiConfig : wifiConfigs)
		{
			if (wifiConfig.SSID.equalsIgnoreCase(ssid))
			{
				netID = wifiConfig.networkId;
			}
		}
		wifiManager.disableNetwork(netID);
		wifiManager.disconnect();
	}

	/**
	 * Get the MAC address of current connection
	 * @return the MAC address string
	 */
	public String getCurrentConnectionMacAddress()
	{
		if (wifiManager == null)
			return null;
		return wifiManager.getConnectionInfo().getMacAddress();
	}

	/**
	 * Get the SSID of current connection
	 * @return the SSID
	 */
	public String getCurrentConnectionSSID()
	{
		if (wifiManager == null)
			return null;
		return wifiManager.getConnectionInfo().getSSID();
	}

	/**
	 * Get the IP address of current connection
	 * @return the IP address, or 0 if failed
	 */
	public int getCurrentConnectionIpAddress()
	{
		if (wifiManager == null)
			return 0;
		return wifiManager.getConnectionInfo().getIpAddress();
	}

	/**
	 * Get the Network ID of current connection
	 * @return the Network ID, 0 if failed
	 */
	public int getCurrentConnectionNetWorkId()
	{
		if (wifiManager == null)
			return 0;
		return wifiManager.getConnectionInfo().getNetworkId();
	}
	
	/**
	 * Register event receiver to receive network change event
	 */
	public void RegisterNetworkEventReceiver(BroadcastReceiver networkEventReceiver)
    {
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(networkEventReceiver, filter);
    }
	
	/**
	 * Unregister event receiver of network change
	 * @param contextWrapper
	 */
	public void UnregisterNetworkEventReceiver(BroadcastReceiver networkEventReceiver)
	{
		context.unregisterReceiver(networkEventReceiver);
	}
	 
	/**
	 * find the BSSID with the best signal
	 * @param SSID the SSID of the AP
	 * @param scanResultList the scan result list got by scan
	 * @return A ScanResult object
	 */
	private ScanResult findTheBestSignalBSSID(String SSID, List<ScanResult> scanResultList)
	{
		int indexBestSignalBSSID = -1;
		int count = scanResultList.size();
		for (int i = 0; i < count; i++)
		{
			ScanResult sr = scanResultList.get(i);
			if (sr.SSID.equals(SSID))
			{
				if (indexBestSignalBSSID <= -1)
				{
					indexBestSignalBSSID = i;
				} else
				{
					if (sr.level > scanResultList.get(indexBestSignalBSSID).level)
					{
						indexBestSignalBSSID = i;
					}
				}
			}
		}
		if (indexBestSignalBSSID >= 0 && indexBestSignalBSSID < count)
			return scanResultList.get(indexBestSignalBSSID);
		else
			return null;
	}

	/**
	 * Create a WiFi Configuration using specified SSID, Password and CipherType
	 * <p>Disclaimer: Partial code comes from author on Internet whom I don't know about.
	 * @param SSID the SSID
	 * @param Password the password
	 * @param Type the Cipher Type
	 * @return A WifiConfiguration object
	 */
	private WifiConfiguration createWifiConfig(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.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
		}
		//WEP
		else 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
		else 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;
	}

	/**
	 * Check if the key string is a Hex WEP key
	 * <p>Disclaimer: This code comes from author on Internet whom I don't know about.
	 * @param wepKey the key string
	 * @return true if it is, false if not
	 */
	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);
	}

	/**
	 * Check if a string is hex digit
	 * <p>Disclaimer: This code comes from author on Internet whom I don't know about.
	 * @param key a string to check
	 * @return true if it's HEX digit, false if not
	 */
	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;
	}

	/**
	 * Find an exiting Wifi Configuration with the name specified by SSID
	 * @param SSID
	 * @return A WifiConfiguration object
	 */
	private WifiConfiguration getExsitConfig(String SSID)
	{
		List<WifiConfiguration> wifiConfiguredNetworks = getConfiguredNetworks();
		if (wifiConfiguredNetworks == null)
			return null;

		for (WifiConfiguration existingConfig : wifiConfiguredNetworks)
		{
			if (existingConfig.SSID.equals("\"" + SSID + "\""))
			{
				return existingConfig;
			}
		}
		return null;
	}

	/**
	 * A internal class to start a new thread within which to connect the AP specified by WifiConfiguration
	 * @author Kyven.zhang@126.com
	 *
	 */
	private class ConnectThread implements Runnable
	{
		private WifiManager wifiManager;
		private WifiConfiguration wifiConfig;
		boolean isNewConfig;

		public ConnectThread(WifiManager wifiManager, WifiConfiguration wifiConfig, boolean isNewConfig)
		{
			this.wifiManager = wifiManager;
			this.wifiConfig = wifiConfig;
			this.isNewConfig = isNewConfig;
		}

		@Override
		public void run()
		{
			if (wifiManager == null || wifiConfig == null)
			{
				return;
			}

			if (isNewConfig)
			{
				int netID = wifiManager.addNetwork(wifiConfig);
				wifiManager.enableNetwork(netID, true);
				wifiManager.reconnect();
			} 
			else
			{
				wifiManager.enableNetwork(wifiConfig.networkId, true);
				wifiManager.reconnect();
			}
		}
	}
	
	/**
	 * A class to receive network change events
	 * <br>User can extend this class and implement the abstract methods to handle the event interested
	 * @author Kyven.zhang@126.com
	 *
	 */
	public class NetworkChangeEventReceiver extends BroadcastReceiver
	{
		@Override
	    public void onReceive(Context context, Intent intent)
	    {
			/** A WiFi scan has completed. */
			if(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction()))
			{
				HandleWifiScanCompletedEvent(wifiManager);
			}
			/** the WiFi state has changed. */
	        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction()))
	        {
	        	int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
	            HandleWiFiStateChangeEvent(wifiState);

	        }  	        
	        /** the WiFi connectivity has changed. */ 
	        else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction()))
	        {
	            Parcelable parcelableExtraNetworkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
	            if (null != parcelableExtraNetworkInfo)
	            {
	                NetworkInfo networkInfo = (NetworkInfo) parcelableExtraNetworkInfo;
		            String bssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
		            WifiInfo wifiInfo = null;
		            Parcelable parcelableExtraWifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);	            
		            if(parcelableExtraWifiInfo != null)
		            {
		            	wifiInfo = (WifiInfo)parcelableExtraWifiInfo;
		            }
	                HandleWiFiConnectivityChangeEvent(networkInfo,bssid,wifiInfo);	                
	            }
	        }
	        /**
	         * network change has happened to WiFi, Mobile Data, Ethernet, Bluetooth, or WiMax.
	         */
	        else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
	        {	        	
	        	ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
	        	HandleSystemConnectivityChangeEvent(connectivityManager);	        	
	        }
	    }
		
		/**
		 * Handle the event of WiFi scan completion
		 * @param wifiManager the WifiManager object from super class WifiAdmin
		 * <p>Override this method in extended class to handle the event, and get the scan result using following code
		 * <br>
		 *		List<ScanResult> wifiList = wifiManager.getScanResults()
		 * 		...
		 */
		public void HandleWifiScanCompletedEvent(WifiManager wifiManager)
		{
		}

    	/**
    	 * Handle the event of WiFi state change.
         * @param wifiState it includes the following 5 states:
         * WifiManager.WIFI_STATE_DISABLING (=0): the WiFi is being turned off
         * WifiManager.WIFI_STATE_DISABLED (=1): the WiFi is turned off
         * WifiManager.WIFI_STATE_ENABLING (=2): the WiFi is being turned on
         * WifiManager.WIFI_STATE_ENABLED (=3): the WiFi is turned on
         * WifiManager.WIFI_STATE_UNKNOWN (=4): the WiFi state is unknown, usually it happens when enabling or disabling, and error occurs.
         * <p>Override this method in extended class to handle the state like below:
         * <br>	 
         *		 switch (wifiState)
	     *       {
	     *        case 0: //WifiManager.WIFI_STATE_DISABLING:
	     *            //do something
	     *            break;
	     *        case 1: //WifiManager.WIFI_STATE_DISABLED:
	     *        	//do something
	     *            break;
	     *        case 2: //WifiManager.WIFI_STATE_ENABLING:
	     *        	//do something
	     *            break;
	     *        case 3: //WifiManager.WIFI_STATE_ENABLED: 
	     *        	//do something
	     *            break;
	     *        case 4: //WifiManager.WIFI_STATE_UNKNOWN:
	     *        	//do something
	     *            break;
	     *        }
	     *
         */	
		public void HandleWiFiStateChangeEvent(int wifiState)
		{		
		}
		
		/**
		 * Handle the event of WiFi connectivity change.
		 * 
		 * @param networkInfo the information of the changed WiFi connection.  	
		 * @param bssid the bssid of the connected AP. ONLY valid when networkInfo.getState() == CONNECTED 
		 * @param wifiInfo more information of the the connected AP. ONLY valid when networkInfo.getState() == CONNECTED
		 * <p>Override this method in extended class to check the new connectivity state and/or check if it's the AP you cared. following is an example.
		 * <br>
		 * 			switch (networkInfo.getState()) //could be CONNECTED, CONNECTING, DISCONNECTED, DISCONNECTING, SUSPENDED, UNKNOWN
	     *           {
	     *          case CONNECTED:
	     *           {
	     *           	if(wifiInfo != null)
	     *           	{
	     *           		if(wifiInfo.getSSID().equalsIgnoreCase("myCaredSSID"))
	     *           			//do something
	     *           	}
	     *               break;
	     *           }
	     *           case CONNECTING:
	     *               //...
	     *           	
	     *           default:
	     *               break;
	     *           }
	     *           
		 */
		public void HandleWiFiConnectivityChangeEvent(NetworkInfo networkInfo, String bssid, WifiInfo wifiInfo)
		{		
		}
		
		/**
		 * Handle any network change event such as WiFi, Mobile Data, Ethernet, Bluetooth, WiMax. 
		 * @param connectivityManager: the ConnectivityManager object. 
		 * Override this method in extended class to use connectivityManager to get the current active network or enumerate all networks to find those interested.
		 * Following is an example.
		 * 		if (connectivityManager != null) 
	     *   	{  
	     *   		//NetworkInfo networkInfo = ConnectivityManager.getActiveNetworkInfo();
	     *   		NetworkInfo[] allNetworkInfo = connectivityManager.getAllNetworkInfo();  
	     *   		for(NetworkInfo networkInfo : allNetworkInfo)
	     *   		{
	     *   			if(networkInfo.getState() == NetworkInfo.State.CONNECTED)
	     *   				//do something
	     *   		}
	     *   	}  
		 * 
		 */
		public void HandleSystemConnectivityChangeEvent(ConnectivityManager connectivityManager)
		{		
		}
	
	}

}

2. WifiAdmin使用示例

最简单的使用情况下,只需要new 一个WifiAdmin对象,然后使用它的相关方法就可以了。

稍微复杂一点情况下,如果需要异步侦听Wifi及网络事件并进行响应处理,则需要扩展WifiAmin类及其内部的事件接收类NetworkChangeEventReceiver,并覆盖相关事件处理函数。

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity
{
	private MyWifiAdmin myWifiAdmin; 
	private Button buttonTest; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);//请修改成实际的activity id
		buttonTest = (Button) findViewById(R.id.test);  //请修改成实际的resource id
		myWifiAdmin = new MyWifiAdmin(this);  
		
		buttonTest.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				/**复杂点的用法,使用异步事件通知
				 * 先注册事件接收器,然后在事件接收器的处理函数中做相应处理
				 */	
				myWifiAdmin.RegisterNetworkEventReceiver(myWifiAdmin.myNetworkChangeEventReceiver);		
				
				/** 简单的用法,比如连接某个AP */
				myWifiAdmin.openWifi(0);
				myWifiAdmin.scan();
				myWifiAdmin.connect("TestAccessPoint", "testpassword");
				
	
			}     	
        });  
	}
	
	/**
	 * Extend WifiAdmin class to override handle methods for network change events
	 * 这是复杂点的用法,如果不需要异步事件通知,则不需要从WifiAdmi扩展此类,直接使用WifiAdmin类对象的相关方法即可
	 *
	 */
	private class MyWifiAdmin extends WifiAdmin
	{
		//New MyNetworkChangeEventReceiver object, which will be registered as the system broadcast receiver.
		public MyNetworkChangeEventReceiver myNetworkChangeEventReceiver = new MyNetworkChangeEventReceiver();	
		public MyWifiAdmin(Context context)
		{
			super(context);
		}
		//Extend class NetworkChangeEventReceiver and override event handler method as you want. 
		//需要处理哪个事件则覆盖那个事件的处理方法,不需要覆盖所有方法
		public class MyNetworkChangeEventReceiver extends NetworkChangeEventReceiver
		{
			@Override
			public void HandleWifiScanCompletedEvent(WifiManager wifiManager)
			{
				// TODO Auto-generated method stub
				Log.d("testactivity", "HandleWifiScanCompletedEvent, completed");
			}

			@Override
			public void HandleWiFiConnectivityChangeEvent(NetworkInfo networkInfo, String bssid, WifiInfo wifiInfo)
			{
				// TODO Auto-generated method stub
				if(wifiInfo != null)
				{
					Log.d("testactivity", "HandleWiFiConnectivityChangeEvent, AP " + wifiInfo.getSSID() + " state: " + networkInfo.getState().toString());
				}
				else
				{
					Log.d("testactivity", "HandleWiFiConnectivityChangeEvent, state: " + networkInfo.getState().toString());
				}
			}
	
			@Override
			public void HandleWiFiStateChangeEvent(int wifiState)
			{
				// TODO Auto-generated method stub
				Log.d("testactivity", "HandleWiFiStateChangeEvent, wifiState=" + wifiState);
			}
			
			@Override
			public void HandleSystemConnectivityChangeEvent(ConnectivityManager connectivityManager)
			{
				// TODO Auto-generated method stub
				if(connectivityManager != null)
				{
					NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
					if(networkInfo != null)
						Log.d("testactivity", "HandleSystemConnectivityChangeEvent, network type=" + networkInfo.getTypeName());
					else
						Log.d("testactivity", "HandleSystemConnectivityChangeEvent, no network info");
				}
				else
				{
					Log.d("testactivity", "HandleSystemConnectivityChangeEvent, connectivityManager is null");
				}
			}
		}
	}	
}

 

3. 权限需求

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

      上面权限中,ACCESS_NETWORK_STATE是为侦听系统网络连接变化ConnectivityManager.CONNECTIVITY_ACTION而使用的,调用connectivityManager.getActiveNetworkInfo时会要求该权限。


4. 版权声明

      本代码可以任意拷贝修改、用于任意目的。代码中有3处Disclaimer声明的代码来至Internet,特此免责说明。作者不对使用本程序可能造成的后果负责。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值