Android实践 -- Android wifi 设置相关

  • WifiManager

    获得WifiManager的实例

    private WifiManager mWifiManager = (WifiManager)c.getSystemService(Context.WIFI_SERVICE);
    
  • 检测Wiif的状态

    Wifi的状态值常量

    WIFI_STATE_DISABLED = 1; //Wi-Fi is disabled.
    WIFI_STATE_DISABLING = 0; //Wi-Fi is currently being disabled
    WIFI_STATE_ENABLED = 3; //Wi-Fi is enabled.
    WIFI_STATE_ENABLING = 2; //Wi-Fi is currently being enabled.
    WIFI_STATE_UNKNOWN = 4; //Wi-Fi is in an unknown state
    

Wifi状态监测

int state = mWifiManager.isWifiEnabled();

state的值,对应上面的wifi状态常量

  • 连接wifi

    打开wifi

    mWifiManager.setWifiEnabled(true);
    

    关闭wifi

    mWifiManager.setWifiEnabled(false);
    
  • Wifi 状态的变化的监听

    Wifi状态的变化可以通过广播来监听

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
      @Override
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){
              int s = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
              //wifi的状态变化会触发这个广播
              //s的值对应上面的wifi状态常量,可以根据不同的状态值来更新界面
          }
      }
    };
    

    链接wifi

    //netId 可以通过添加一个WifiConfiguration获得
    int netId = mWifiManager.addNetwork(config);
    boolean b = mWifiManager.enableNetwork(netId, true);
    
    

    添加WifiConfiguration
    //ssid : wifi 的名称,pwd: wifi的密码 ,type: 加密方式 0:没有加密 1:wep 2: wap

    public WifiConfiguration createWifiInfo(String ssid,String pwd,int type){
      log.e("create wifif ssid: "+ssid+" pwd: "+pwd+" type "+type);
      WifiConfiguration cfg = new WifiConfiguration();
      WifiConfiguration tempConfig = this.isExist(ssid);
      if(tempConfig != null) {
          mWifiManger.removeNetwork(tempConfig.networkId);
      }
    
      cfg.SSID = "\"" + ssid + "\"";
      if( !TextUtils.isEmpty(pwd)) {
          if(type == 0){
              cfg.wepKeys[0] = "";
              cfg.allowedKeyManagement.set(0);
              cfg.wepTxKeyIndex = 0;
          }else if( type == 1 ) {
              cfg.wepKeys[0]   = "\"" + pwd + "\"";
              cfg.wepTxKeyIndex = 0;
          }else {
              cfg.preSharedKey = "\"" + pwd + "\"";
          }
      }
    
      return cfg;
    }
    

    wifi断开连接

    mWifiManager.disableNetwork(netId);
    

    删除已经保存的wifi

    boolean r = mWifiManager.removeNetwork(netId);
    
    
  • 扫描可用的Wifi

    Wifi连接之后,可以扫描当前可以使用的wifi

    boolean scan = mWifiManager.startScan(); //返回true,说明扫描成功
    

    获取扫描到的wifi列表

    List scanResults = mWifiManager.getScanResults();
    

    通过ScanResult可以获得wifi的相关信息

    for(ScanResult sr : scanResults){
      String SSID = sr.SSID; //wifi的名称,在设置中显示的名称
      String BSSID = sr.BSSID; //wifi的地址
      String cap = sr.capabilities; //wifi的加密方式
      int freq = sr.frequency ; //wifi的频率
      int level = sr.level; //wifi的强度
    }
    

    计算wifi的强度

    int level = WifiManager.calculateSignalLevel(sr.level, 5);
    
  • 获取已经连接的wifi的信息

    WifiInfo wi = mWifiManger.getConnectionInfo();
    

WifiInfo 的信息如图


Android实践 -- Android wifi 设置相关_第1张图片
这里写图片描述
  • 获取已经配置好的wifi的信息

    List configs = mWifiManager.getConfiguredNetworks();
    

WifiConfiguration的信息如图


Android实践 -- Android wifi 设置相关_第2张图片
这里写图片描述

Android实践 -- Android wifi 设置相关_第3张图片
这里写图片描述

你可能感兴趣的:(Android实践 -- Android wifi 设置相关)