在看一下代码之前需要简单了解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中的
这个界面一般包括
一个switchbar(控制开关,在WifiEnaber中实现),控制wifi的开关,
主要用WifiEnabler中的onSwitchChanged方法中实现
调用wifiManager的setWifiEnabled(boolean ischeck)方法进行开关
preferenceScreen(用来显示ap(如路由器)列表)
OptionsMenu
选项菜单,通过sethasOptionsMenu(true)会自动调用oncreateOptionsMenu方法,
方法中调用addOptionsMenuItems进行初始
包括新增网络,保存的网络,刷新,高级(会有条件具体显示的菜单,如通过savedNetworksExist来判断“保存的网络”是否显示在菜单上)
ContextMenu
长按ap会弹出内容菜单,通过RegisterForContextMenu(listview),会自动调用OnCreateContextMenu方法,
包括连接,忘记,修改,写入NFC的功能(会有条件具体显示的菜单,如连接的,保存的,未连接的的ap)
这些方法在wifiSettings中都有具体的实现代码,可以分析
WifiSettings位于packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java
其中有两个类,其中的Multimap为多重映射,在constructAccessPoints方法中会被调用
Multimap
apMap用来存放ssid与accesspoint的键值,其中,相同的键可以有多个值(意味着可能存在ssid相同的多个ap)
负责扫描,发送消息扫描,间隔10秒,startScan(),连续三次扫描都失败就停止扫描。这个类在WifiSettings构造方法中被初始化,
private static class Scanner extends Handler {
176 private int mRetry = 0;
177 private WifiSettings mWifiSettings = null;
178
179 Scanner(WifiSettings wifiSettings) {
180 mWifiSettings = wifiSettings;
181 }
182
183 void resume() {
184 if (!hasMessages(0)) {
185 sendEmptyMessage(0);
186 }
187 }
188
189 void forceScan() {
190 removeMessages(0);
191 sendEmptyMessage(0);
192 }
193
194 void pause() {
195 mRetry = 0;
196 removeMessages(0);
197 }
198
199 @Override
200 public void handleMessage(Message message) {
201 if (mWifiSettings.mWifiManager.startScan()) {
202 mRetry = 0; //当中有一次扫描成功mRetry=0;
203 } else if (++mRetry >= 3) { //开始扫描的操作失败mRetry+1与3比较,超过三次就return
204 mRetry = 0;
205 Activity activity = mWifiSettings.getActivity();
206 if (activity != null) {
207 Toast.makeText(activity, R.string.wifi_fail_to_scan, Toast.LENGTH_LONG).show();
208 }
209 return;
210 }
211 sendEmptyMessageDelayed(0, WIFI_RESCAN_INTERVAL_MS); //每隔10秒,发起扫描的操作
212 }
213 }
214
在wifiSettings构造方法中,增加了Intent过滤器和广播接受者,其中广播接受者的时间在HandleEvent(Intent intent)中处理
但是我有一个问题:为啥过滤器中注册了8个Action
public WifiSettings() {
216 super(DISALLOW_CONFIG_WIFI);
217 mFilter = new IntentFilter();
218 mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
219 mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
220 mFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
221 mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
222 mFilter.addAction(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION);
223 mFilter.addAction(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION);
224 mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
225 mFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
226
227 mReceiver = new BroadcastReceiver() {
228 @Override
229 public void onReceive(Context context, Intent intent) {
230 handleEvent(intent);
231 }
232 };
233
234 mScanner = new Scanner(this);
235 }
但是在
HandleEvent方法中却只有
6个action的处理,NETWORK_IDS_CHANGED_ACTION与SUPPLICANT_STATE_CHANGED_ACTION却没有处理,那么加入的目的?
HandleEvent处理广播的代码:
777 private void handleEvent(Intent intent) {
778 String action = intent.getAction();
779 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
780 updateWifiState(intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
781 WifiManager.WIFI_STATE_UNKNOWN)); //更新wifi状态改变,Enabled Enabling Disabled
782 } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) ||
783 WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) ||
784 WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) {
785 updateAccessPoints(); //更新AccessPoints
786 } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
787 NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
788 WifiManager.EXTRA_NETWORK_INFO);
789 mConnected.set(info.isConnected());
790 changeNextButtonState(info.isConnected());
791 updateAccessPoints();
792 updateNetworkInfo(info); //更新ap再更新网络信息
793 } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) {
794 updateNetworkInfo(null);
795 }
796 }
797
不同的action对应处理不同的事件:
WIFI_STATE_CHANGED_ACTION:当wifi状态改变的时候,updataWifiState(int state),根据不同的状态,做不同的处理:
827 private void updateWifiState(int state) {
828 Activity activity = getActivity();
829 if (activity != null) {
830 activity.invalidateOptionsMenu();
831 }
832
833 switch (state) {
834 case WifiManager.WIFI_STATE_ENABLED:
835 mScanner.resume(); //enabled的时候,发送扫描信息startScan()
836 return; // not break, to avoid the call to pause() below //避免调用mScanner.pause()停止扫描
837
838 case WifiManager.WIFI_STATE_ENABLING:
839 addMessagePreference(R.string.wifi_starting); //加入“正在打开wifi”
840 break;
841
842 case WifiManager.WIFI_STATE_DISABLED:
843 setOffMessage(); //wifi不可用的时候,显示一些其他信息(通过provider判断)
844 break;
845 }
846
847 mLastInfo = null;
848 mLastNetworkInfo = null;
849 mScanner.pause(); //停止扫描
850 }
第二个的判断条件中有三个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的动作表明连接的配置已经改变
在这些情况下,都会更新wifi的信息updateAccessPoints();
640 private void updateAccessPoints() {
641 // Safeguard from some delayed event handling
642 if (getActivity() == null) return;
643
644 if (isUiRestricted())
645 addMessagePreference(R.string.wifi_empty_list_user_restricted); //判断是否有限制
646 return;
647 }
648 final int wifiState = mWifiManager.getWifiState();
649
650 //when we update the screen, check if verbose logging has been turned on or off
651 mVerboseLogging = mWifiManager.getVerboseLoggingLevel();
652
653 switch (wifiState) { //根据wifi状态来处理
654 case WifiManager.WIFI_STATE_ENABLED: //当wifi状态可用的情况下
655 // AccessPoints are automatically sorted with TreeSet.
656 final Collection accessPoints =
657 constructAccessPoints(getActivity(), mWifiManager, mLastInfo,
658 mLastNetworkInfo); //主要通过constructAccessPoints进行更新
659 getPreferenceScreen().removeAll();
660 if (accessPoints.size() == 0) {
661 addMessagePreference(R.string.wifi_empty_list_wifi_on); //如果ap没有,则显示“正在搜索wlan网络”
662 }
663
664 for (AccessPoint accessPoint : accessPoints) {
665 // Ignore access points that are out of range.
666 if (accessPoint.getLevel() != -1) {
667 getPreferenceScreen().addPreference(accessPoint); //遍历,增加到preferenceScreen中(可以阅读相关资料了解)
668 }
669 }
670 break;
671
672 case WifiManager.WIFI_STATE_ENABLING:
673 getPreferenceScreen().removeAll(); //enabling的情况下,移出preferenceScreen中所有的ap
674 break;
675
676 case WifiManager.WIFI_STATE_DISABLING:
677 addMessagePreference(R.string.wifi_stopping); //显示“正在关闭“
678 break;
679
680 case WifiManager.WIFI_STATE_DISABLED: //不可用的时候,显示其他信息
681 setOffMessage();
682 break;
683 }
684 }
685
798 private void updateNetworkInfo(NetworkInfo networkInfo) {
799 /* sticky broadcasts can call this when wifi is disabled */
800 if (!mWifiManager.isWifiEnabled()) {
801 mScanner.pause();
802 return;
803 }
804
805 if (networkInfo != null &&
806 networkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR) {
807 mScanner.pause();
808 } else {
809 mScanner.resume();
810 }
811
812 mLastInfo = mWifiManager.getConnectionInfo();
813 if (networkInfo != null) {
814 mLastNetworkInfo = networkInfo;
815 }
816 //倒序更新AccessPoint的信息,应该是更新了修改配置之后的ap的信息
817 for (int i = getPreferenceScreen().getPreferenceCount() - 1; i >= 0; --i) {
818 // Maybe there's a WifiConfigPreference
819 Preference preference = getPreferenceScreen().getPreference(i);
820 if (preference instanceof AccessPoint) {
821 final AccessPoint accessPoint = (AccessPoint) preference;
822 accessPoint.update(mLastInfo, mLastNetworkInfo);
823 }
824 }
825 }
之前看到在updataAccessPoints中调用了这个方法:constructAccessPoints,看一下这个方法的源码把
private static List constructAccessPoints(Context context,
719 WifiManager wifiManager, WifiInfo lastInfo, NetworkInfo lastNetworkInfo) {
720 ArrayList accessPoints = new ArrayList(); //存放ap的ArrayList,用来返回
721 /** Lookup table to more quickly update AccessPoints by only considering objects with the
722 * correct SSID. Maps SSID -> List of AccessPoints with the given SSID. */
723 Multimap apMap = new Multimap();//多重映射,键SSID(网络名)--值(ap)
724
725 final List configs = wifiManager.getConfiguredNetworks();//获取手机中保存过配置的连接信息
726 if (configs != null) {
727 // Update "Saved Networks" menu option. //更新选项“saved Networks”的状态
728 if (savedNetworksExist != (configs.size() > 0)) { //比较式前后一致的返回值,更新Saved Networks的菜单选项
//例如:没有存储的wifi信息,那么savedNetworksExist为flase
//例如:有存储的wifi信息,那么savedNetworksExist为true
729 savedNetworksExist = !savedNetworksExist;
730 if (context instanceof Activity) {
731 ((Activity) context).invalidateOptionsMenu(); //刷新optionMenu
732 }
733 }
734 for (WifiConfiguration config : configs) { //对配置过的信息进行遍历
735 if (config.selfAdded && config.numAssociation == 0) { // Number of time we associated to this configuration
736 continue; //跳过本次循环
737 }
738 AccessPoint accessPoint = new AccessPoint(context, config);
739 if (lastInfo != null && lastNetworkInfo != null) {
740 accessPoint.update(lastInfo, lastNetworkInfo);
741 }
742 accessPoints.add(accessPoint); //把ap加入List中
743 apMap.put(accessPoint.ssid, accessPoint);
744 }
745 }
746
747 final List results = wifiManager.getScanResults(); //wifi扫描结果的的处理
748 if (results != null) {
749 for (ScanResult result : results) { //遍历扫描结果
750 // Ignore hidden and ad-hoc networks. //忽略隐藏的(没有SSID)以及ad-hoc(IBSS?)
751 if (result.SSID == null || result.SSID.length() == 0 ||
752 result.capabilities.contains("[IBSS]")) {
753 continue;
754 }
755
756 boolean found = false; //第一次的apMap中存放了配置过的信息
757 for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
758 if (accessPoint.update(result)) //判断扫描结果ssid和security安全协议是否存在过,存在更新
759 found = true;
760 }
761 if (!found) { //未找到的情况下,加入accesspoints的
762 AccessPoint accessPoint = new AccessPoint(context, result);
763 if (lastInfo != null && lastNetworkInfo != null) {
764 accessPoint.update(lastInfo, lastNetworkInfo);
765 }
766 accessPoints.add(accessPoint);
767 apMap.put(accessPoint.ssid, accessPoint); //放入apMap,再次遍历会调用
768 }
769 }
770 }
771
772 // Pre-sort accessPoints to speed preference insertion
773 Collections.sort(accessPoints);
774 return accessPoints;
775 }
来了解一下所有的重写的方法:OptionsMenu:调用addOptionsMenuItems方法进行初始化
397 void addOptionsMenuItems(Menu menu) { //这里的通过wifi是否打开的状态设置menu中元素的enable状态
398 final boolean wifiIsEnabled = mWifiManager.isWifiEnabled();
399 TypedArray ta = getActivity().getTheme().obtainStyledAttributes(
400 new int[] {R.attr.ic_menu_add, R.attr.ic_wps});
401 menu.add(Menu.NONE, MENU_ID_ADD_NETWORK, 0, R.string.wifi_add_network)
402 .setIcon(ta.getDrawable(0))
403 .setEnabled(wifiIsEnabled) //增加网络,当wifi不可用的时候为false,不能点击
404 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
405 if (savedNetworksExist) {
406 menu.add(Menu.NONE, MENU_ID_SAVED_NETWORK, 0, R.string.wifi_saved_access_points_label)
407 .setIcon(ta.getDrawable(0))
408 .setEnabled(wifiIsEnabled) //通过判断是否存在保存的网络,来决定显示与否
409 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
410 }
411 menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.menu_stats_refresh)
412 .setEnabled(wifiIsEnabled)
413 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);//刷新,通过判断wifi状态来决定是否能刷新
414 menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
415 .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //高级选项
416 ta.recycle();
417
OptionMenuItems对应的点击事件为:onOptionsItemSelected,根据不同的item点开对应的dialog,
重写onActivityCreated(参数)
1.获得系统服务WifiManager
2.注册监听,connect,save,forget
3.savedInstanceState的状态判断,进行一些初始化
@Override
238 public void onActivityCreated(Bundle savedInstanceState) {
239 super.onActivityCreated(savedInstanceState);
240 //获得服务
241 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
242 //三种监听
243 mConnectListener = new WifiManager.ActionListener() {
244 @Override
245 public void onSuccess() {
246 }
247 @Override
248 public void onFailure(int reason) {
249 Activity activity = getActivity();
250 if (activity != null) {
251 Toast.makeText(activity,
252 R.string.wifi_failed_connect_message,
253 Toast.LENGTH_SHORT).show();
254 }
255 }
256 };
257
258 mSaveListener = new WifiManager.ActionListener() {
259 @Override
260 public void onSuccess() {
261 }
262 @Override
263 public void onFailure(int reason) {
264 Activity activity = getActivity();
265 if (activity != null) {
266 Toast.makeText(activity,
267 R.string.wifi_failed_save_message,
268 Toast.LENGTH_SHORT).show();
269 }
270 }
271 };
272
273 mForgetListener = new WifiManager.ActionListener() {
274 @Override
275 public void onSuccess() {
276 }
277 @Override
278 public void onFailure(int reason) {
279 Activity activity = getActivity();
280 if (activity != null) {
281 Toast.makeText(activity,
282 R.string.wifi_failed_forget_message,
283 Toast.LENGTH_SHORT).show();
284 }
285 }
286 };
287
288 if (savedInstanceState != null) {
289 mDlgEdit = savedInstanceState.getBoolean(SAVE_DIALOG_EDIT_MODE);
290 if (savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {
291 mAccessPointSavedState =
292 savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);
293 }
294 }
295
296 // if we're supposed to enable/disable the Next button based on our current connection
297 // state, start it off in the right state
298 Intent intent = getActivity().getIntent();
299 mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
300
301 if (mEnableNextOnConnection) {
302 if (hasNextButton()) {
303 final ConnectivityManager connectivity = (ConnectivityManager)
304 getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
305 if (connectivity != null) {
306 NetworkInfo info = connectivity.getNetworkInfo(
307 ConnectivityManager.TYPE_WIFI);
308 changeNextButtonState(info.isConnected());
309 }
310 }
311 }
312
313 addPreferencesFromResource(R.xml.wifi_settings);
314
315 mEmptyView = initEmptyView();
316 registerForContextMenu(getListView()); //这样会调用onCreateContextMenu(ContextMenu, View, ContextMenuInfo) 的方法
317 setHasOptionsMenu(true); //会调用onCreateOptionsMenu
318
319 if (intent.hasExtra(EXTRA_START_CONNECT_SSID)) {
320 String ssid = intent.getStringExtra(EXTRA_START_CONNECT_SSID);
321 updateAccessPoints();
322 PreferenceScreen preferenceScreen = getPreferenceScreen();
323 for (int i = 0; i < preferenceScreen.getPreferenceCount(); i++) {
324 Preference preference = preferenceScreen.getPreference(i);
325 if (preference instanceof AccessPoint) {
326 AccessPoint accessPoint = (AccessPoint) preference;
327 if (ssid.equals(accessPoint.ssid) && accessPoint.networkId == -1
328 && accessPoint.security != AccessPoint.SECURITY_NONE) {
329 onPreferenceTreeClick(preferenceScreen, preference);
330 break;
331 }
332 }
333 }
334 }
335 }
336
重写onstart(参数)
对WifiEnabler进行创建对象:主要是对switchbar进行操作
在wifiEnabler的构造方法中,加入intent-filter来接受广播
346 @Override
347 public void onStart() {
348 super.onStart();
349
350 // On/off switch is hidden for Setup Wizard (returns null)
351 mWifiEnabler = createWifiEnabler();
352 }
重写onResume(参数)
做了三件事情:1加入switchbar的广播注册,加入switch的监听
2.WifiSettings注册广播
3更新ap
362 @Override
363 public void onResume() {
364 final Activity activity = getActivity();
365 super.onResume();
366 if (mWifiEnabler != null) {
367 mWifiEnabler.resume(activity);
368 }
369
370 activity.registerReceiver(mReceiver, mFilter);
371 updateAccessPoints();
372 }
重写onPause(参数)
做了三件事:1wifiEnabler中解除广播注册,移出switch监听
2.WifiSettings移出广播
3.停止扫描移出scanner中的message
374 @Override
375 public void onPause() {
376 super.onPause();
377 if (mWifiEnabler != null) {
378 mWifiEnabler.pause();
379 }
380
381 getActivity().unregisterReceiver(mReceiver);
382 mScanner.pause();
383 }
重写onDestroyView(参数):
1.隐藏switchbar:teardownSwitchbar()
337 @Override
338 public void onDestroyView() {
339 super.onDestroyView();
340
341 if (mWifiEnabler != null) {
342 mWifiEnabler.teardownSwitchBar();
343 }
344 }