管理Wifi——WifiManager

作者:追风剑情 发布于:2016-8-17 13:01 分类:Android

WifiManager代表Android Wi-Fi连接服务。它能够用于配置Wi-Fi网络连接、管理当前Wi-Fi连接、扫描接入点以及监视Wi-Fi连接的变化。

使用WifiManager需要配置以下权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

示例代码

//获取Wi-Fi Manager
String service = Context.WIFI_SERVICE;
WifiManager wifi = (WifiManager)getSystemService(service);

//如果Wifi没开启
if(!wifi.isWifiEnabled()){
	if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING)
		wifi.setWifiEnabled(true);//启用wifi
}

 

监听Wi-Fi状态变化

//监听Wifi硬件状态(关闭、开启、...)
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
//监听与接入点之间的连接状态(新连接建立或者现有连接丢失)
filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
//监听Wifi当前网络状态
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
//监听Wifi信号强度变化
filter.addAction(WifiManager.RSSI_CHANGED_ACTION);

registerReceiver(new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
			String action = intent.getAction();
			
			//上一次的wifi状态
			int wifiStatePre = intent.getIntExtra(  
					WifiManager.EXTRA_PREVIOUS_WIFI_STATE,  
					WifiManager.WIFI_STATE_DISABLED);
			
			//新的wifi状态
			int wifiStateNew = intent.getIntExtra(  
					WifiManager.EXTRA_WIFI_STATE,  
					WifiManager.WIFI_STATE_DISABLED);
		
			if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){
				Log.d("test", "WIFI_STATE_CHANGED_ACTION");
				
				//处理各种wifi状态
				switch(wifiStateNew){
					case WifiManager.WIFI_STATE_DISABLING:
						break;
					case WifiManager.WIFI_STATE_DISABLED:
						break;
					case WifiManager.WIFI_STATE_ENABLING:
						break;
					case WifiManager.WIFI_STATE_ENABLED:
						break;
					case WifiManager.WIFI_STATE_UNKNOWN:
						break;
				}
				
			}
			else if(action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
			{
				Log.d("test", "SUPPLICANT_CONNECTION_CHANGE_ACTION");
				boolean state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
				if(state){
					//TODO: 建立新连接
				}else{
					//TODO: 现有连接丢失
				}
				Log.d("test", "state="+state);
			}
			else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
			{
				Log.d("test", "NETWORK_STATE_CHANGED_ACTION");
				
				String bssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
				NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
			}
			else if(action.equals(WifiManager.RSSI_CHANGED_ACTION))
			{
				Log.d("test", "RSSI_CHANGED_ACTION");
				
				WifiInfo info = wifi.getConnectionInfo();
				if(info.getBSSID() == null)
					return;
				//Wifi信号强度
				//int rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0);
				//Wifi信号强度
				int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
				//Wifi的名称
				String ssid = info.getSSID();
				//Wifi的MAC地址
				String bssid = info.getBSSID();
				//链接速度
				int speed = info.getLinkSpeed();
				//链接速度单位(Mbps)
				String units = WifiInfo.LINK_SPEED_UNITS;
				
				String cSummary = String.format("Connected to %s at %s%s. Strength %s/5", ssid, speed, units, strength);
				Log.d("test", cSummary);
			}
		}
	},
	filter);

 

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号