26
2017
09

工作随笔---WiFi开发(4)---WIFISavedList类

1.前言

前面已经介绍了wifi scanList数据获取以及两种不同加密方式的热点连接,现在我们通过WIFIUtil类 里面的封装好的接口来搭建保存的WiFi热点画面以及连接已保存的热点连接方式。

2.开发流程

(1)在onResume()时,通过handler来获取已保存的热点列表并刷新信号强度。

private SysManagerWIFIUtil m_wifiUtil = null;
private SYS_VIEW_MENU_COM_WifiAPUtil m_wifiAPUtil = null;

private List<WifiRegisteredAP> m_registeredAP = new ArrayList<WifiRegisteredAP>();
private List<ScanResult> m_scannedAP = new ArrayList<ScanResult>();
private List<String> m_scanLevelList = new ArrayList<String>(); //(String = ssid+security+level)
private List<WifiConfigurationAP> m_configurationList = new ArrayList<WifiConfigurationAP>();
new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    m_wifiUtil.startScan();
                    getAllNetWorkList();
                    int connectingID = m_wifiAPUtil.getNetID();
                    itempos = netIDToPosition(connectingID);
                    SysLog.out(TAG, "Handler", "itempos="+itempos);

                    m_listView.getList().notifyDataSetChanged();
                }
            }, 225);
        }

startTimer();
    private synchronized void getAllNetWorkList() {
        if (m_scannedAP == null) {
            m_scannedAP = new ArrayList<ScanResult>();
        }

        if(m_registeredAP == null){
            m_registeredAP = new ArrayList<WifiRegisteredAP>();
        }

        for(int i=0;i<SYS_VIEW_MENU_COM_WifiConfigurationDBUtil.DATAKEY.length;i++){
            WifiConfigurationAP ap = SYS_VIEW_MENU_COM_WifiConfigurationDBUtil.getInstance(m_Context).getWifiConfigurationFromDataManager(SYS_VIEW_MENU_COM_WifiConfigurationDBUtil.DATAKEY[i]);
            if(DataManager.E_DATA_MGR_REQ_RES_SUCCESS == SYS_VIEW_MENU_COM_WifiConfigurationDBUtil.getInstance(m_Context).getResultOfGetData(SYS_VIEW_MENU_COM_WifiConfigurationDBUtil.DATAKEY[i])){
                SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList","DATAKEY["+i+"] has data,add"+", apid="+ap.getNetid());
                if(!m_configurationList.contains(ap)){
                    m_configurationList.add(ap);
                }
            }else{
                SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList","DATAKEY["+i+"] has not data,can not add!!");
            }
        }

        m_scannedAP = m_wifiUtil.getWifiList();
        WifiRegisteredAP m_wifiRegisteredAP = null;
        List<WifiConfiguration> configuration = m_wifiUtil.getConfiguration();
        if (null != configuration) {
            for (WifiConfiguration config : configuration) {
                if (null != config) {
                    m_wifiRegisteredAP = new WifiRegisteredAP();
                    m_wifiRegisteredAP.setmConfig(config);
                    m_wifiRegisteredAP.setSignalLevel(-100);
                    for (WifiConfigurationAP ap : m_configurationList) {
                        SysLog.out(
                                TAG,
                                "COM_RegisteredAP getAllNetWorkList",
                                "netid="
                                        + config.networkId
                                        + ", apid="
                                        + Integer.parseInt(ap.getNetid())
                                        + ", SSID="
                                        + config.SSID
                                        + ", timestamp="
                                        + m_wifiUtil
                                                .getTimestamp(config.networkId));
                        if (0 < Integer.parseInt(ap.getTimestamp())) {
                            if (config.networkId == Integer.parseInt(ap
                                    .getNetid())) {
                                m_wifiRegisteredAP.setRegisteredAPBssid(ap
                                        .getBssid());
                                m_wifiRegisteredAP.setTimestamp(Integer
                                        .parseInt(ap.getTimestamp()));
                                m_registeredAP.add(m_wifiRegisteredAP);
                            } else {
                                // SYS_VIEW_MENU_COM_WifiConfigurationDBUtil.getInstance(m_Context).deleteWifiConfigurationFromDataManager(Integer.parseInt(ap.getNetid()));
                                continue;
                            }
                        }
                    }

                } else {
                    SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList",
                            "config is null !!!");
                }
            }

            loadWiFiAPInfo();

        }else{
            SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList", "WifiConfiguration is null !!!" );
        }

        if (m_wifiAPUtil.isPriorityAdjusted()) {
            m_registeredAP = m_wifiAPUtil.getM_registeredAP();
            m_wifiAPUtil.setPriorityAdjusted(false);
        }

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

        m_wifiAPUtil.setM_registeredAP(m_registeredAP);

        SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList",
                m_registeredAP.size() + "");

        for(WifiConfigurationAP ap:m_configurationList){
            if(null != ap){
                SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList","id="+ap.getNetid()+",bssid="+ap.getBssid()+", ssid="+ap.getSsid()+", priority="+ap.getPriority()+", autoflag="+ap.getAutoconnectflag()+", timestamp="+ap.getTimestamp()+", type="+ap.getSecuritytype());
            }else{
                SysLog.out(TAG, "COM_RegisteredAP getAllNetWorkList","ap = null");
            }
        }
    }

    @SuppressWarnings("rawtypes")
    private class MyCompartor implements Comparator {

        @Override
        public int compare(Object arg0, Object arg1) {
            WifiRegisteredAP previousAP = (WifiRegisteredAP) arg0;
            WifiRegisteredAP nextAP = (WifiRegisteredAP) arg1;
            int prepriority = m_wifiUtil.getAutoConnectPriority(previousAP
                    .getmConfig().networkId);
            int nextpriority = m_wifiUtil.getAutoConnectPriority(nextAP
                    .getmConfig().networkId);
            return Integer.valueOf(nextpriority).compareTo(
                    Integer.valueOf(prepriority));
        }

    }
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_wifiUtil.startScan();
                m_handler.sendEmptyMessage(MESSAGEOK);
            }
        };
        if (m_task != null && m_timer != null) {
            m_timer.schedule(m_task, 6000, 6000);
        }
    }

private Handler m_handler = new Handler() {

        @SuppressLint("SimpleDateFormat")
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGEOK:
                getUpdateList();
                m_listView.getList().notifyDataSetChanged();
                SysLog.out(
                        TAG,
                        "COM_RegisteredAP handleMessage",
                        "current time:"
                                + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                                        .format(new Date()));
                break;
            default:
                break;
            }
        };
    };
    private synchronized void getUpdateList(){

        m_scanLevelList.clear();

        m_scannedAP = m_wifiUtil.getWifiList();

        loadWiFiAPInfo();

        SysLog.out(TAG, "COM_RegisteredAP UpdateList",
                m_registeredAP.size() + "");

    }
    private void loadWiFiAPInfo(){
        m_scannedAP = m_wifiUtil.getWifiList();
        if(null != m_scannedAP){
            for (ScanResult result : m_scannedAP) {
                String Ssid = result.SSID;
//              String Bssid = result.BSSID;
                int level = result.level;
                int securityType = m_wifiUtil.wifiType(result.capabilities);
                if(m_wifiAdmin.judgeSSID(Ssid)){
//                  SysLog.out(TAG, "COM_RegisteredAP getUpdateList","ssid="+Ssid+", securityType="+securityType+", level="+level);
                    String temp = Ssid+";"+securityType+";"+level;
                    m_scanLevelList.add(temp);
                }
            }
        }

        if (null != m_registeredAP) {
            for (int i = 0; i < m_registeredAP.size(); i++) {
                String apssid = m_wifiUtil.dealSSID(m_registeredAP.get(i).getmConfig().SSID);
                int aptype = m_wifiUtil.getSecurityType(m_registeredAP.get(i)
                        .getmConfig());
                if ((-1 != aptype) && (null != apssid)) {
                    for(String s: m_scanLevelList){
                        String[] str = s.split(";");
                        String ssid = m_wifiUtil.dealSSID(str[0]);
                        int type = Integer.parseInt(str[1]);
                        int level = Integer.parseInt(str[2]);
//                      SysLog.out(TAG, "COM_RegisteredAP loadWiFiAPInfo", "apsssid="
//                              + apssid + ", ssid="+ssid+", scType=" + type+", aptype="+aptype+", level="+level);
                        if(apssid.equals(ssid) && type == aptype){
                            m_registeredAP.get(i).setSignalLevel(level);
                            break;
                        }else{
                            m_registeredAP.get(i).setSignalLevel(-100);
                        }
                    }

                }
            }

        }
    }

(2)连接已保存的热点或者断开正在连接的热点

    //当前有正在连接的热点,断开wifi连接
    if(!(m_wifiUtil.getBSSID()).equals(DEFAULTBSSID)){
            m_wifiUtil.disconnectWifi();
            SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition", "itempos="+itempos);
            if(0 <= itempos){
                CTL_16AVHN_L_710_wifi_Button item=(CTL_16AVHN_L_710_wifi_Button)m_listView.getList().getItemAtPos(itempos);
                if(null != item){
                    item.setImage(CTL_16AVHN_L_710_wifi_Button.IMAGEID_SIGNAL, 0);
                    item.setDisConnect();
                }
            }
            SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition",
                    "position="+position+", disconnect: "  + wrAP.getmConfig().SSID);

    } else {
            //无信号,连接失败
            if (-100 >= wrAP.getSignalLevel()) {
                SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition",
                        "AP is InActive ,signal=" + wrAP.getSignalLevel());
                m_wifiUtil.disconnectWifi();
            } else {
                WifiConfiguration wificonfig = wrAP.getmConfig();
                if(null == wificonfig){
                    SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition", "wificonfig is null !!");
                    return;
                }
                //已保存的热点直接通过netid连接
                boolean con = m_wifiUtil.connectConfiguration(wificonfig.networkId);
                m_wifiAPUtil.setNetID(wrAP.getmConfig().networkId);
                SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition",
                        "position="+position+", connect: " + wrAP.getmConfig().SSID);

                SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition",
                        "startConnect"+">con"+con);
                //连接建立,开启连接中
                if (con) {

                    final CTL_16AVHN_L_710_wifi_Button item=(CTL_16AVHN_L_710_wifi_Button)m_listView.getList().getItemAtPos(position);
                    item.setImage(CTL_16AVHN_L_710_wifi_Button.IMAGEID_SIGNAL, 0);

                    SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition",
                            "item="+item.isConnect());

                    SysLog.out(TAG, "COM_RegisteredAP onItemClickForIDPosition",
                            "item="+item.isConnect());

                    SysManagerCommonIF.instance().dealWifiConnectTimeOut();

                    m_isManually = true;

                    SysManagerCommonIF.instance().m_managerCommon.setOnTimeOutListener(new OnTimeOutListener() {
                        @Override
                        public void func() {
                            SysLog.out(TAG, "onItemClickForIDPosition", "isTimeOut");
                            m_isConnecting = false;
                            m_isManually = false;
                            setTitleButtonEnable(m_registeredAP, true);
                            m_listView.getList().notifyDataSetChanged();
                            SysLog.out(TAG, "onItemClickForIDPosition", "m_isConnecting="+m_isConnecting);
                        }
                    });


                } else {
                    SysLog.out(TAG,
                            "COM_RegisteredAP onItemClickForIDPosition",
                            "connect failure");
                }
            }


    }
    m_listView.getList().notifyDataSetChanged();
上一篇:SpringBoot发邮件-注意事项 下一篇:工作随笔---WiFi开发(5)---WIFIWpsConnect类