Android-WiFi连接-修改

使用WiFi工具类遇到问题及修改

参考了博客: 参考博客.

在参考博客给的代码上进行了修改,先放上原代码


/**
     * WiFi连接管理
     * 申请权限
     * 
     * 
     * 
     * 
     * 动态权限
     * Manifest.permission.ACCESS_COARSE_LOCATION
     * Manifest.permission.ACCESS_FINE_LOCATION
     *
     */
    public class WifiUtils {
     
        private static WifiUtils utils = null;

        public WifiUtils(Context context){
     
            wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        }

        public static WifiUtils getInstance(Context context){
     
            if (utils == null){
     
                synchronized (WifiUtils.class){
     
                    if (utils == null){
     
                        utils = new WifiUtils(context);
                    }
                }
            }
            return utils;
        }

        private WifiManager wifiManager;

        /**
         * wifi是否打开
         * @return
         */
        public boolean isWifiEnable(){
     
            boolean isEnable = false;
            if (wifiManager != null){
     
                if (wifiManager.isWifiEnabled()){
     
                    isEnable = true;
                }
            }
            return isEnable;
        }

        /**
         * 打开WiFi
         */
        public void openWifi(){
     
            if (wifiManager != null && !isWifiEnable()){
     
                wifiManager.setWifiEnabled(true);
            }
        }

        /**
         * 关闭WiFi
         */
        public void closeWifi(){
     
            if (wifiManager != null && isWifiEnable()){
     
                wifiManager.setWifiEnabled(false);
            }
        }

        /**
         * 获取WiFi列表
         * @return
         */
        public List<ScanResult> getWifiList(){
     
            wifiManager.startScan();
            List<ScanResult> resultList = new ArrayList<>();
            if (wifiManager != null && isWifiEnable()){
     
                resultList.addAll(wifiManager.getScanResults());
            }
            return resultList;
        }

        /**
         * 有密码连接
         * @param ssid
         * @param pws
         */
        public void connectWifiPws(String ssid, String pws){
     

            wifiManager.disableNetwork(wifiManager.getConnectionInfo().getNetworkId());
            int netId = wifiManager.addNetwork(getWifiConfig(ssid, pws, true));
            wifiManager.enableNetwork(netId, true);
        }

        /**
         * 无密码连接
         * @param ssid
         */
        public void connectWifiNoPws(String ssid){
     
            wifiManager.disableNetwork(wifiManager.getConnectionInfo().getNetworkId());
            int netId = wifiManager.addNetwork(getWifiConfig(ssid, "", false));
            wifiManager.enableNetwork(netId, true);
        }

        /**
         * wifi设置
         * @param ssid
         * @param pws
         * @param isHasPws
         */
        private WifiConfiguration getWifiConfig(String ssid, String pws, boolean isHasPws){
     
            WifiConfiguration config = new WifiConfiguration();
            config.allowedAuthAlgorithms.clear();
            config.allowedGroupCiphers.clear();
            config.allowedKeyManagement.clear();
            config.allowedPairwiseCiphers.clear();
            config.allowedProtocols.clear();
            config.SSID = "\"" + ssid + "\"";

            WifiConfiguration tempConfig = isExist(ssid);
            if(tempConfig != null) {
     
                wifiManager.removeNetwork(tempConfig.networkId);
            }
            if (isHasPws){
     
                config.preSharedKey = "\""+pws+"\"";
                config.hiddenSSID = true;
                config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                config.status = WifiConfiguration.Status.ENABLED;
            }else {
     
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            }
            return config;
        }

        /**
         * 得到配置好的网络连接
         * @param ssid
         * @return
         */
        private WifiConfiguration isExist(String ssid) {
     
            List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration config : configs) {
     
                if (config.SSID.equals("\""+ssid+"\"")) {
     
                    return config;
                }
            }
            return null;
        }
    }

上面的工具类写的很整齐,很方便使用。但是存在问题,我使用了-有密码连接-时出现了问题。进行了修改(如果上面的可以用,还是用上面的吧):

/**
         * 有密码连接
         * @param ssid
         * @param pws
         */
        public void connectWifiPws(String ssid, String pws){
     

        WifiConfiguration conf = new WifiConfiguration();
        //正确的格式是  \"TP-Link\"  
            String ssid1 = "\"" + ssid + "\"";
            conf.SSID = ssid1;
            conf.preSharedKey = "\""+ pws +"\"";

           //链接wifi
            wifiManager.addNetwork(conf);
            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration i : list) {
     
                if (i.SSID != null && i.SSID.equals(ssid1)) {
     
 //                   wifiManager.disconnect();
                    wifiManager.enableNetwork(i.networkId, true);
                    wifiManager.reconnect();
                    break;
                }
            }
       }

你可能感兴趣的:(Android-WiFi连接-修改)