工作随笔---WiFi开发(3)---WIFIScanList类

1.前言

前两篇已经为后面开发做好了准备工作,现在我们通过WIFIUtil类 里面的封装好的接口来搭建WiFi热点检索画面,然后通过输入密码来连接热点。

2.开发流程

(1).在画面onResume()时,通过handler来启动热点扫描以及数据刷新。

public List<ScanResult> m_scannedAP = new ArrayList<ScanResult>();
private Timer m_timer = null;
private TimerTask m_task = null;
private SysManagerWIFIUtil m_wifiUtil = null;

    new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    m_wifiUtil.startScan();
                    getUpdateNetWorkList(); // get scannedAP List
                    m_listView.notifyDataSetChanged();
                }
    }, 100);

    startTimer();//定时刷新画面上热点
    public  synchronized void getUpdateNetWorkList() {
        m_scannedAP.clear();
        if (m_scannedAP == null) {
            m_scannedAP = new ArrayList<ScanResult>();
        }
        List<ScanResult> result = m_wifiUtil.getWifiList();
        //获取已经保存的热点列表
        List<WifiRegisteredAP> device = m_wifiAPUtil.getM_registeredAP();
        SysLog.out(TAG, "getUpdateNetWorkList", "registeredAPList="+device.size());
        if (null != device) {

            for(int i = 0;i<device.size();i++){
                WifiRegisteredAP reAp = device.get(i);
                if (null != reAp) {
                    String SSID = reAp.getmConfig().SSID;
                    String BSSID = reAp.getRegisteredAPBssid();
                    int TYPE = m_wifiUtil.getSecurityType(reAp.getmConfig());
                    int timestamp = reAp.getTimestamp();
                    if (null != result) {
                        for (int j = 0; j < result.size(); j++) {
                            ScanResult scAp = result.get(j);
                            if (null != scAp) {
                                String scBssid = scAp.BSSID;
                                String scSsid = scAp.SSID;
                                int scType = m_wifiUtil.wifiType(scAp.capabilities);
                                if ((-1 != scType) && (null != scSsid) && (null != scBssid)) {
//                                  SysLog.out(TAG, "SYS_VIEW_MENU_COM_ScannedAP  getUpdateNetWorkList",
//                                          "scType=" + scType+", Type="+TYPE+", scSsid="+scSsid+", SSID="+m_wifiUtil.dealSSID(SSID)+", scBssid="+scBssid+", BSSID="+BSSID);
                                    if ((scType == TYPE)
                                            && scSsid.equals(m_wifiAdmin.dealSSID(SSID))
                                            && (0 < timestamp) 
                                            && scBssid.equals(BSSID)) {
                                         result.remove(j);
                                         break;

                                    }else{
                                        continue;
                                    }
                                }

                            }

                        }

                    }

                }

            }

        }else{
            SysLog.out(TAG, "SYS_VIEW_MENU_COM_ScannedAP  getAllNetWorkList", "saved device is null !!!");
        }

        for (ScanResult sc : result) {
            if (null != sc) {
                String scSsid = sc.SSID;
                if (((scSsid.length() >= 1) && (scSsid.length() <= 32))
                        && (m_wifiUtil.judgeSSID(scSsid))) {
                    m_scannedAP.add(sc);
                }
            }
        }

        Collections.sort(m_scannedAP, new MyCompartor());

        m_wifiAPUtil.setM_scannedAP(m_scannedAP);

        SysLog.out(TAG, "SYS_VIEW_MENU_COM_ScannedAP  getAllNetWorkList",
                "scannedAPSize=" + m_scannedAP.size());
    }
    private class MyCompartor implements Comparator {

        @Override
        public int compare(Object arg0, Object arg1) {
            ScanResult previousAP = (ScanResult) arg0;
            ScanResult nextAP = (ScanResult) arg1;
            int prelevel = m_wifiAPUtil
                    .calculateSignalLevel(previousAP.level, 5);
            int nextlevel = m_wifiAPUtil.calculateSignalLevel(nextAP.level, 5);
            if (prelevel != nextlevel) {
                return Integer.valueOf(nextlevel).compareTo(
                        Integer.valueOf(prelevel));
            } else {
                return previousAP.SSID.compareTo(nextAP.SSID);
            }
        }

    }
    private void removeTimer(){
        if (m_task != null) {
            m_task.cancel();
            m_task = null;
        }
        if (m_timer != null) {
            m_timer.cancel();
            m_timer = null;
        }
    }

    private void startTimer(){
        m_timer = new Timer();
        m_task = new TimerTask() {
            @Override
            public void run() {
                m_wifiAPUtil.startScan();
                m_handler.sendEmptyMessage(MESSAGEAUTOUPDATE);
            }
        };
        if (m_task != null && m_timer != null) {
            m_timer.schedule(m_task, 6000, 6000);
        }
    }

    private Handler m_handler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGEAUTOUPDATE:
                getUpdateNetWorkList();
                m_listView.notifyDataSetChanged();
                SysLog.out(
                        TAG,
                        "COM_ScannedAP handleMessage",
                        "current time:"
                                + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                                        .format(new Date()));
                break;

            default:
                break;
            }
        };
    };

(2).画面正常显示扫描到的热点后,可以根据自己的选择Wifi热点,匹配正确的密码,连接wifi。连接热点时区分无加密热点和有加密的热点。

①无加密的热点

            m_wifiUtil.disconnectWifi();
            int isconnect = m_wifiUtil.addNetwork(scResult.SSID, "",
                    m_wifiUtil.wifiType(scResult.capabilities));
            m_wifiAPUtil.setScBssid(scResult.BSSID);

            SysLog.out(TAG, "COM_ScannedAP listViewListener", "netID="
                    + isconnect);
            if (isconnect > -1) {
                int netID = isconnect;
                m_wifiAPUtil.setNetID(netID);
                SysManagerCommonIF.instance().dealWifiConnectTimeOut();
            } else {
                SysLog.out(TAG,
                        "COM_ScannedAP onItemClickForIDPosition",
                        "connect failure");
            }

②有加密的热点

                    if (WifiManager.WIFI_STATE_ENABLED == m_wifiUtil.checkState()) {
                        m_wifiUtil.disconnectWifi();
                        int sta = m_wifiUtil.addNetwork(
                                m_scanedAPList.get(pos).SSID, input,
                                wifiType(m_scanedAPList.get(pos).capabilities));
                        m_wifiAPUtil.setScBssid(m_scanedAPList
                                .get(pos).BSSID);

                        setScreenType();

                        if (sta > -1) {
                            SysLog.out(TAG,
                                    "COM_PassWordEntry IButtonActionListener",
                                    "netWorkID=" + sta);
                            netID = sta;
                            m_wifiAPUtil.setNetID(netID);
                            m_wifiAPUtil.setPasswordForward(true);

                        } 
                        else {
                            SysLog.out(TAG,
                                    "COM_PassWordEntry IButtonActionListener",
                                    "connect failure(input error)");
                        }
                    }

猜你喜欢

转载自blog.csdn.net/qq_20160723/article/details/78064123
今日推荐