Android Wi-Fi 系统源码wifiSettings源码分析(Android 5.1 Base)

一.在看一下代码之前需要简单了解wifi的基本知识

802.11 协议 : wifi 用到的是 802.11b,802.11g( 是 802.11b 的 后继标准 )
station :携带无线网卡的设备,如智能手机,笔记本 , 底层会启动 wpa-supplicant: 实现 station 对无线网络的管理和控制功能。

AP : accesspoint 本身也是一个 station, 能为关联的 STA 提供分布式 服务( ds ),如路由器

DS : distributionservice: 分布式服务 ,BSS 和 LAN 组合在一起构成 一个 ESS 的就是 ds,ds 一般是指有线网络(通过它接入互联网)

BSS : BasicService Set, 是由上述原件组成的网络

基础结构型 BSS : 通常是指的 Infrastructurebasic Service Set, 有 AP参与。

独立型 BSS : 通常是指 IndependentBSS, 不需要 AP, 各个 STA 直接互 联,自组网络对等网络,通常我们所说的 BSS 是指基础结构型

ESS : ExtendedService Set 扩展服务集,包含一个或者多个 BSS.

SSID : ServiceSet Identification: 网络名

BSSID : 在基础结构型网络中,他就是 ap 的 MAC 地址,在独立型 BSS 中为随机生成,

wpa-supplicant : 使得无线网卡工作在 managed 模式,

softap : 软 AP 底层启动 :hostapd 的后台管理进程, 常见的为 hotspot

hostapd : 切换为 master 模式,模拟 AP, 建立一个无线开放的网络,

在谷歌提供的安卓源码中,网址如下:http://androidxref.com ,初学者学习,分析,留疑问,并且长期更新,修改错误,补充。

二.安卓的系统wifi模块,一般在设置----->Wifi中WifiSettings显示的就是打开wifi的那个界面

需要先了解一些wifi模块的API如WifiManager类等。

WifiSettings继承SettingsPreferenceFragment,具有fragement的生命周期(可百度看一下)如sethasOptionsMenu(true)这方法是Fragment中的.界面一般包括

1.switchbar (控制开关,在WifiEnaber中实现),控制wifi的开关,

主要用WifiEnabler中的onSwitchChanged方法中实现调用 wifiManager 的setWifiEnabled(boolean ischeck)方法进行开关

 preferenceScreen(用来显示ap(如路由器)列表)
  
  

2.OptionsMenu

选项菜单,通过 sethasOptionsMenu(true) 会自动调用oncreateOptionsMenu方法,方法中调用addOptionsMenuItems进行初始

包括新增网络,保存的网络,刷新,高级( 会有条件具体显示的菜单 ,如通过savedNetworksExist来判断“保存的网络”是否显示在菜单上)

3.ContextMenu

长按AP会弹出内容菜单,通过RegisterForContextMenu(listview),会自动调用OnCreateContextMenu方法.

包括连接,忘记,修改,写入NFC的功能(会有条件具体显示的菜单,如连接的,保存的,未连接的的ap)

三.这些方法在wifiSettings中都有具体的实现代码,可以分析

WifiSettings位于packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java

其中有两个类,其中的Multimap为多重映射,在 constructAccessPoints方法中会被调用

Multimap<String,AccessPoint> apMap = new Multimap<String, AccessPoint>()
  
  

apMap 用来存放 ssid 与 accesspoint 的键值,其中,相同的键可以有多个值(意味着可能存在 ssid 相同的多个 ap )

1.负责扫描,发送消息扫描,间隔 10 秒, startScan(), 连续三次扫描都失败就停止扫描。这个类在WifiSettings构造方法中被初始化.

private static class Scanner extends Handler {    
  
  
    private int mRetry = 0
    private WifiSettings mWifiSettings = null
  
    Scanner(WifiSettings wifiSettings) {
        mWifiSettings = wifiSettings; 
    } 
  
    void resume() {   
        if (!hasMessages(0)) {             
          sendEmptyMessage(0); 
        } 
    }

  void forceScan() {
    removeMessages(0); 
    sendEmptyMessage(0); 
  }

  void pause() {
    mRetry = 0;
    removeMessages(0);
  }
  
  @Override 
    public void handleMessage(Message message) { 
      if (mWifiSettings.mWifiManager.startScan()) {
        mRetry = 0;                                       //当中有一次扫描成功mRetry=0
      } else if (++mRetry >= 3) {                         //开始扫描的操作失败mRetry+1与3比较,超过三次就return
        mRetry = 0
        Activity activity = mWifiSettings.getActivity(); 
        if (activity != null) {
        Toast.makeText(activity, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show(); 
      }
     return
    } 
    sendEmptyMessageDelayed(0, WIFI_RESCAN_INTERVAL_MS); //每隔10秒,发起扫描的操作
    } 
  } 

2.在wifiSettings构造方法中,增加了Intent过滤器和广播接受者,其中广播接受者的时间在HandleEvent(Intent intent)中处理,但是我有一个问题:为啥过滤器中注册了8个Action.

public WifiSettings() {
  
  
  super(DISALLOW_CONFIG_WIFI);
    mFilter = new IntentFilter();       
    mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);    
    mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
    mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);    
    mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION); 
    mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
    mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);

  mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      handleEvent(intent);
    }
  };
  mScanner = new Scanner(this);
}

疑问:但是在HandleEvent方法中却只有6个action的处理,NETWORK_IDS_CHANGED_ACTION与SUPPLICANT_STATE_CHANGED_ACTION却没有处理,那么加入的目的?

3.HandleEvent处理广播的代码

private void handleEvent(Intent intent) {
  
  
  String action = intent.getAction();
  if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
    updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
    WifiManager.WIFI_STATE_UNKNOWN));                           //更新wifi状态改变,Enabled Enabling Disabled
  } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
    WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) ||
    WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) {
    updateAccessPoints();                                           //更新AccessPoints 
  } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
    NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( 
    WifiManager.EXTRA_NETWORK_INFO); 
    mConnected.set(info.isConnected()); 
    changeNextButtonState(info.isConnected()); 
    updateAccessPoints(); 
    updateNetworkInfo(info);                                       //更新ap再更新网络信息                                                               
  } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
    updateNetworkInfo(null); 
  }

4.不同的action对应处理不同的事件

WIFI_STATE_CHANGED_ACTION:当wifi状态改变的时候,updataWifiState(int state),根据不同的状态,做不同的处理:

private void updateWifiState(int state) {
  
  
  Activity activity = getActivity();
  if (activity != null) {
    activity.invalidateOptionsMenu();
  }
  switch (state) {
    case WifiManager.WIFI_STATE_ENABLED:
    mScanner.resume();                                //enabled的时候,发送扫描信息startScan()
    return// not break, to avoid the call to pause() below  //避免调用mScanner.pause()停止扫描
    
    case WifiManager.WIFI_STATE_ENABLING: 
    addMessagePreference(R.string.wifi_starting);    //加入“正在打开wifi”
    break
    
    case WifiManager.WIFI_STATE_DISABLED:
    setOffMessage();                                //wifi不可用的时候,显示一些其他信息(通过provider判断)
    break;
  }

  mLastInfo = null;
  mLastNetworkInfo = null;
  mScanner.pause();                                      //停止扫描
}

第二个的判断条件中有三个action:分别对应的是
SCAN_RESULTS_AVAILABLE_ACTION
An access point scan has completed, and results are available from the supplicant.
一个AP扫描完成,并且从supplicant获得的结果是可用的

CONFIGURED_NETWORKS_CHANGED_ACTION
Broadcast intent action indicating that the configured networks changed. This can be as a result of adding/updating/deleting a network
广播intent的动作表明配置的网络已经改变,比如增加/更新/删除一个网络

LINK_CONFIGURATION_CHANGED_ACTION
Broadcast intent action indicating that the link configuration changed on wifi
广播intent的动作表明连接的配置已经改变

5.在这些情况下,都会更新wifi的信息updateAccessPoints();

private void updateAccessPoints() {
  
  
  // Safeguard from some delayed event handling
  if (getActivity() == nullreturn;

  if (isUiRestricted())
    addMessagePreference(R.string.wifi_empty_list_user_restricted);     //判断是否有限制
    return
  }
  final int wifiState = mWifiManager.getWifiState();

  //when we update the screen, check if verbose logging has been turned on or off 
  mVerboseLogging = mWifiManager.getVerboseLoggingLevel(); 

  switch (wifiState) {                                                  //根据wifi状态来处理
    case WifiManager.WIFI_STATE_ENABLED:                              //当wifi状态可用的情况下 

    // AccessPoints are automatically sorted with TreeSet.
    final Collection<AccessPoint> accessPoints =
    constructAccessPoints(getActivity(), mWifiManager, mLastInfo,
    mLastNetworkInfo);                           //主要通过constructAccessPoints进行更新
    getPreferenceScreen().removeAll(); 
    if (accessPoints.size() == 0) {
      addMessagePreference(R.string.wifi_empty_list_wifi_on); //如果ap没有,则显示“正在搜索wlan网络” 
    }

    for (AccessPoint accessPoint : accessPoints) {
      // Ignore access points that are out of range. 
      if (accessPoint.getLevel() != -1) { 
        getPreferenceScreen().addPreference(accessPoint);  //遍历,增加到preferenceScreen中(可以阅读相关资料了解)
      } 
    } 
    break

    case WifiManager.WIFI_STATE_ENABLING: 
      getPreferenceScreen().removeAll();                         //enabling的情况下,移出preferenceScreen中所有的ap
    break

    case WifiManager.WIFI_STATE_DISABLING: 
      addMessagePreference(R.string.wifi_stopping);             //显示“正在关闭“ 
    break

    case WifiManager.WIFI_STATE_DISABLED:                        //不可用的时候,显示其他信息
      setOffMessage(); 
    break
  } 

这里面最主要的方法就是constructAccessPoints这个方法了,之后在学习把。。NETWORK_STATE_CHANGED_ACTION
Broadcast intent action indicating that the state of Wi-Fi connectivity has changed. One extra provides the new statewifi
连通性被改变,提供了新的状态更新ap的同时,更新NetworkInfo,在安卓5.0的情况下名字为updateConnectionState(估计认为这是网络状态?怕理解成connected的ap???)

private void updateNetworkInfo(NetworkInfo networkInfo) {
  
  
  /* sticky broadcasts can call this when wifi is disabled */ 
  if (!mWifiManager.isWifiEnabled()) {
    mScanner.pause();
    return
  }

  if (networkInfo != null && 
    networkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR) {
    mScanner.pause();
  } else {
    mScanner.resume();
  }

  mLastInfo = mWifiManager.getConnectionInfo(); 
    if (networkInfo != null) {
    mLastNetworkInfo = networkInfo;
  }
  //倒序更新AccessPoint的信息,应该是更新了修改配置之后的ap的信息
  for (int i = getPreferenceScreen().getPreferenceCount() - 1; i >= 0; --i) {
    // Maybe there's a WifiConfigPreference
    Preference preference = getPreferenceScreen().getPreference(i);
    if (preference instanceof AccessPoint) {
      final AccessPoint accessPoint = (AccessPoint) preference;
      accessPoint.update(mLastInfo, mLastNetworkInfo);
    }
  }
}

RSSI_CHANGED_ACTION
The RSSI (signal strength) has changed.
显而易见,这指的是信号强度被改变的action,调用updateNetworkInfo,更新一下网络信息就可以了

以上是handleEvent的处理

=============================================================================================================
之前看到在updataAccessPoints中调用了这个方法:constructAccessPoints,看一下这个方法的源码把

private static List<AccessPoint> constructAccessPoints(Context context,
  
  
  WifiManager wifiManager, WifiInfo lastInfo, NetworkInfo lastNetworkInfo) {
    ArrayList<AccessPoint> accessPoints = new ArrayList<AccessPoint>();       //存放ap的ArrayList,用来返回
    /** Lookup table to more quickly update AccessPoints by only considering objects with the
    * correct SSID.  Maps SSID -> List of AccessPoints with the given SSID.  */ 
    Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>();//多重映射,键SSID(网络名)--值(ap)

    final List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();//获取手机中保存过配置的连接信息 
    if (configs != null) { 
    // Update "Saved Networks" menu option.                //更新选项“saved Networks”的状态
      if (savedNetworksExist != (configs.size() > 0)) {      //比较式前后一致的返回值,更新Saved Networks的菜单选项                                                                       //例如:没有存储的wifi信息,那么savedNetworksExist为flase                                                                       //例如:有存储的wifi信息,那么savedNetworksExist为true
        savedNetworksExist = !savedNetworksExist;
        if (context instanceof Activity) {
        ((Activity) context).invalidateOptionsMenu();   //刷新optionMenu
        }
      }
      for (WifiConfiguration config : configs) {                   //对配置过的信息进行遍历
        if (config.selfAdded && config.numAssociation == 0) {    // Number of time we associated to this configuration
          continue;                                            //跳过本次循环
        }
        AccessPoint accessPoint = new AccessPoint(context, config);
        if (lastInfo != null && lastNetworkInfo != null) {
          accessPoint.update(lastInfo, lastNetworkInfo);
        }
        accessPoints.add(accessPoint);                          //把ap加入List中
        apMap.put(accessPoint.ssid, accessPoint);
      }
    }

    final List<ScanResult> results = wifiManager.getScanResults();   //wifi扫描结果的的处理 
    if (results != null) {
    for (ScanResult result : results) {                          //遍历扫描结果
      // Ignore hidden and ad-hoc networks.                    //忽略隐藏的(没有SSID)以及ad-hoc(IBSS?) 
      if (result.SSID == null || result.SSID.length() == 0 || 
      result.capabilities.contains("[IBSS]")) {
        continue
      } 

      boolean found = false;                                  //第一次的apMap中存放了配置过的信息
      for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
        if (accessPoint.update(result))                     //判断扫描结果ssid和security安全协议是否存在过,存在更新
        found = true;
      }
      if (!found) {                                            //未找到的情况下,加入accesspoints的
        AccessPoint accessPoint = new AccessPoint(context, result); 
        if (lastInfo != null && lastNetworkInfo != null) { 
          accessPoint.update(lastInfo, lastNetworkInfo); 
        } 
        accessPoints.add(accessPoint); 
        apMap.put(accessPoint.ssid, accessPoint);           //放入apMap,再次遍历会调用
      }
    }
  } 

  // Pre-sort accessPoints to speed preference insertion 
  Collections.sort(accessPoints); 
  return accessPoints; 
}

==============================================================================================================

7.来了解一下所有的重写的方法:OptionsMenu:调用addOptionsMenuItems方法进行初始化

void addOptionsMenuItems(Menu menu) {       //这里的通过wifi是否打开的状态设置menu中元素的enable状态
  
  
  final boolean wifiIsEnabled = mWifiManager.isWifiEnabled();
  TypedArray ta = getActivity().getTheme().obtainStyledAttributes(
  new int[] {R.attr.ic_menu_add, R.attr.ic_wps}); 
  menu.add(Menu.NONE, MENU_ID_ADD_NETWORK, 0, R.string.wifi_add_network)
  .setIcon(ta.getDrawable(0))
  .setEnabled(wifiIsEnabled)          //增加网络,当wifi不可用的时候为false,不能点击
  .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
  if (savedNetworksExist) {
    menu.add(Menu.NONE, MENU_ID_SAVED_NETWORK, 0, R.string.wifi_saved_access_points_label)
    .setIcon(ta.getDrawable(0))
    .setEnabled(wifiIsEnabled)       //通过判断是否存在保存的网络,来决定显示与否
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); 
  }
  menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.menu_stats_refresh)
  .setEnabled(wifiIsEnabled)
  .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);//刷新,通过判断wifi状态来决定是否能刷新
  menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
  .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //高级选项 
  ta.recycle(); 
}

OptionMenuItems对应的点击事件为:onOptionsItemSelected,根据不同的item点开对应的dialog,当然,onCrateContextMenu中对应的点击事件为onContextItemSelected,根据不同的选项执行不同的操作。当然还有一些重写的方法,那就是生命周期的重写:如:

8.先执行构造方法,方法如下:

8.1 重写onActivityCreated(参数)

1.获得系统服务WifiManager
2.注册监听,connect,save,forget
3.savedInstanceState的状态判断,进行一些初始化

@Override 
  
  
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState); 
  //获得服务
  mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
  //三种监听
    mConnectListener = new WifiManager.ActionListener() {
      @Override 
      public void onSuccess() { 
      } 
      @Override 
      public void onFailure(int reason) { 
        Activity activity = getActivity(); 
        if (activity != null) {
          Toast.makeText(activity, R.string.wifi_failed_connect_message, Toast.LENGTH_SHORT).show(); 
      } 
    } 
  }; 

mSaveListener = new WifiManager.ActionListener() {
    @Override 
    public void onSuccess() { 
    } 
    @Override 
    public void onFailure(int reason) {
      Activity activity = getActivity(); 
        if (activity != null) { 
          Toast.makeText(activity,R.string.wifi_failed_save_message, Toast.LENGTH_SHORT).show(); 
      } 
    } 
  }; 

  mForgetListener = new WifiManager.ActionListener() {
    @Override
    public void onSuccess() {
    } 
    @Override 
    public void onFailure(int reason) {
      Activity activity = getActivity(); 
      if (activity != null) { 
        Toast.makeText(activity, R.string.wifi_failed_forget_message,  Toast.LENGTH_SHORT).show();
      } 
    } 
  }; 

  if (savedInstanceState != null) {
    mDlgEdit = savedInstanceState.getBoolean(SAVE_DIALOG_EDIT_MODE);
    if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
      mAccessPointSavedState =
      savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);
    } 
  } 

  // if we're supposed to enable/disable the Next button based on our current connection
  // state, start it off in the right state
  Intent intent = getActivity().getIntent(); 
  mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false); 

  if (mEnableNextOnConnection) {
    if (hasNextButton()) {
      final ConnectivityManager connectivity = (ConnectivityManager)
      getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 
        if (connectivity != null) {
          NetworkInfo info = connectivity.getNetworkInfo( ConnectivityManager.TYPE_WIFI); 
          changeNextButtonState(info.isConnected()); 
        } 
    } 
  } 

  addPreferencesFromResource(R.xml.wifi_settings); 

  mEmptyView = initEmptyView();
  registerForContextMenu(getListView()); //这样会调用onCreateContextMenu(ContextMenu, View, ContextMenuInfo) 的方法
  setHasOptionsMenu(true);                     //会调用onCreateOptionsMenu

  if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) {
    String ssid = intent.getStringExtra(EXTRA_START_CONNECT_SSID);
    updateAccessPoints();
    PreferenceScreen preferenceScreen = getPreferenceScreen();
      for (int i = 0; i < preferenceScreen.getPreferenceCount(); i++) { 
        Preference preference = preferenceScreen.getPreference(i); 
        if (preference instanceof AccessPoint) {
            AccessPoint accessPoint = (AccessPoint) preference; 
            if (ssid.equals(accessPoint.ssid) && accessPoint.networkId == -1
            && accessPoint.security != AccessPoint.SECURITY_NONE) { 
              onPreferenceTreeClick(preferenceScreen, preference); 
            break
            } 
        } 
      } 
    }
  }

8.2 重写onstart(参数)

对WifiEnabler进行创建对象:主要是对switchbar进行操作
在wifiEnabler的构造方法中,加入intent-filter来接受广播

@Override
  
  
public void onStart() {
  super.onStart(); 

  // On/off switch is hidden for Setup Wizard (returns null)
  mWifiEnabler = createWifiEnabler();
}

8.3 重写onResume(参数)
做了三件事情:
1.加入switchbar的广播注册,加入switch的监听
2.WifiSettings注册广播
3.更新ap

@Override
  
  
public void onResume() {
  final Activity activity = getActivity();
  super.onResume();
  if (mWifiEnabler != null) {
    mWifiEnabler.resume(activity);
  }

  activity.registerReceiver(mReceiver, mFilter);
  updateAccessPoints();
}

8.4 重写onPause(参数)
做了三件事:
1.wifiEnabler中解除广播注册,移出switch监听
2.WifiSettings移出广播
3.停止扫描移出scanner中的message

@Override
  
  
public void onPause() {
  super.onPause(); 
  if (mWifiEnabler != null) {
    mWifiEnabler.pause(); 
  } 

  getActivity().unregisterReceiver(mReceiver);
  mScanner.pause();
}

8.5 重写onDestroyView(参数)
1.隐藏switchbar:teardownSwitchbar()

 @Override
  
  
 public void onDestroyView() {
   super.onDestroyView(); 

   if (mWifiEnabler != null) {
     mWifiEnabler.teardownSwitchBar();
   } 
 }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值