管理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" />

示例代码

  1. //获取Wi-Fi Manager
  2. String service = Context.WIFI_SERVICE;
  3. WifiManager wifi = (WifiManager)getSystemService(service);
  4.  
  5. //如果Wifi没开启
  6. if(!wifi.isWifiEnabled()){
  7. if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING)
  8. wifi.setWifiEnabled(true);//启用wifi
  9. }

 

监听Wi-Fi状态变化

  1. //监听Wifi硬件状态(关闭、开启、...)
  2. filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  3. //监听与接入点之间的连接状态(新连接建立或者现有连接丢失)
  4. filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  5. //监听Wifi当前网络状态
  6. filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  7. //监听Wifi信号强度变化
  8. filter.addAction(WifiManager.RSSI_CHANGED_ACTION);
  9.  
  10. registerReceiver(new BroadcastReceiver(){
  11. @Override
  12. public void onReceive(Context context, Intent intent) {
  13. WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
  14. String action = intent.getAction();
  15. //上一次的wifi状态
  16. int wifiStatePre = intent.getIntExtra(
  17. WifiManager.EXTRA_PREVIOUS_WIFI_STATE,
  18. WifiManager.WIFI_STATE_DISABLED);
  19. //新的wifi状态
  20. int wifiStateNew = intent.getIntExtra(
  21. WifiManager.EXTRA_WIFI_STATE,
  22. WifiManager.WIFI_STATE_DISABLED);
  23. if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){
  24. Log.d("test", "WIFI_STATE_CHANGED_ACTION");
  25. //处理各种wifi状态
  26. switch(wifiStateNew){
  27. case WifiManager.WIFI_STATE_DISABLING:
  28. break;
  29. case WifiManager.WIFI_STATE_DISABLED:
  30. break;
  31. case WifiManager.WIFI_STATE_ENABLING:
  32. break;
  33. case WifiManager.WIFI_STATE_ENABLED:
  34. break;
  35. case WifiManager.WIFI_STATE_UNKNOWN:
  36. break;
  37. }
  38. }
  39. else if(action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
  40. {
  41. Log.d("test", "SUPPLICANT_CONNECTION_CHANGE_ACTION");
  42. boolean state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
  43. if(state){
  44. //TODO: 建立新连接
  45. }else{
  46. //TODO: 现有连接丢失
  47. }
  48. Log.d("test", "state="+state);
  49. }
  50. else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
  51. {
  52. Log.d("test", "NETWORK_STATE_CHANGED_ACTION");
  53. String bssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
  54. NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  55. }
  56. else if(action.equals(WifiManager.RSSI_CHANGED_ACTION))
  57. {
  58. Log.d("test", "RSSI_CHANGED_ACTION");
  59. WifiInfo info = wifi.getConnectionInfo();
  60. if(info.getBSSID() == null)
  61. return;
  62. //Wifi信号强度
  63. //int rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, 0);
  64. //Wifi信号强度
  65. int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
  66. //Wifi的名称
  67. String ssid = info.getSSID();
  68. //Wifi的MAC地址
  69. String bssid = info.getBSSID();
  70. //链接速度
  71. int speed = info.getLinkSpeed();
  72. //链接速度单位(Mbps)
  73. String units = WifiInfo.LINK_SPEED_UNITS;
  74. String cSummary = String.format("Connected to %s at %s%s. Strength %s/5", ssid, speed, units, strength);
  75. Log.d("test", cSummary);
  76. }
  77. }
  78. },
  79. filter);

 

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号