当前位置:操作系统 > 安卓/Android >>

Android Wifi work station Framework and Architecture

Android Wifi work station Framework and Architecture

with wpa_supplicant 0.8.X, BCM4329.

转载请注明出处。

Settings/Wifi UI part structure
WifiSettings是主对话框

167

168    @Override

169    public void onActivityCreated(Bundle savedInstanceState) {

170        // We don't call super.onActivityCreated() here, since it assumes we already set up

171        // Preference (probably in onCreate()), while WifiSettings exceptionally set it up in

172        // this method.

173

174       mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

175       mWifiManager.asyncConnect(getActivity(), new WifiServiceHandler());

176        if (savedInstanceState != null

177                && savedInstanceState.containsKey(SAVE_DIALOG_ACCESS_POINT_STATE)) {

178            mDlgEdit = savedInstanceState.getBoolean(SAVE_DIALOG_EDIT_MODE);

179            mAccessPointSavedState = savedInstanceState.getBundle(SAVE_DIALOG_ACCESS_POINT_STATE);

180        }

1101     public void asyncConnect(Context srcContext, Handler srcHandler) {

1102        mAsyncChannel.connect(srcContext, srcHandler, getMessenger());

1103     }

establish a half connect between WifiManager and WifiService.

1117    public void connectNetwork(WifiConfiguration config) {

1118        if (config == null) {

1119            return;

1120        }

1121        mAsyncChannel.sendMessage(CMD_CONNECT_NETWORK, config);

1122    }

Activity创建的时候会建立和WifiService的AsyncChannel的连接,WifiService同样会建立一个AsyncChannelHandler来处理来自这个Channel的命令消息。AsyncChannel的所谓异步主要用来传递比较耗时的操作并把结果返回给原请求者。AsyncChannel两端分别有一个MessageHandler,srcHandler请求destHandler,destHandler把结果发回给srcHandler,主要是用于处理比较耗时的操作且在WifiManager中处理返回结果。

严格来说,在WifiService的Inte易做图ce方法之外再做出一个通道提供额外的方法并不是一个什么好的设计。命令是可以通过同步接口发送给WifiService的,但是WifiService的处理结果如果通过broadcast或intent给WifiManager,则又解耦的过度;如果WifiManager实现一个binder做event sink,又有点小题大做,所以这儿引入这么个AsyncChannel实在是不得以而为之。

WifiSettings界面使能Wifi时会使用定时器请求扫描,获取扫描结果,列出AP列表。Scanner使用定时器,周期性向WifiService请求主动扫描,定时原理是发出本次扫描请求后延迟1s发送下次请求。相关代码如下:

    private class Scanner extends Handler {

        private int mRetry = 0;

 

        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 (mWifiManager.startScanActive()) {

                mRetry = 0;

            } else if (++mRetry >= 3) {

                mRetry = 0;

                Toast.makeText(getActivity(), R.string.wifi_fail_to_scan,

                        Toast.LENGTH_LONG).show();

                return;

            }

            sendEmptyMessageDelayed(0, WIFI_RESCAN_INTERVAL_MS);

        }

    }

列出AP列表的代码如下:

In WifiSettings.java

        final List<ScanResult> results = mWifiManager.getScanResults(); //同步操作

        if (results != null) {

            for (ScanResult result : results) {

                // Ignore hidden and ad-hoc networks.

               if (result.SSID == null || result.SSID.length() == 0 || result.capabilities.contains("[IBSS]")) {

                   continue;

               }

                boolean found = false;

                for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {

                    if (accessPoint.update(result))

                        found = true;

                }

                if (!found) {

                    AccessPoint accessPoint = new AccessPoint(getActivity(), result);

                    accessPoints.add(accessPoint);

                    apMap.put(accessPoint.ssid, accessPoint);

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,