码迷,mamicode.com
首页 > 移动开发 > 详细

Android 常见的工具类

时间:2015-11-18 00:47:38      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
/**
 * Wifi 管理类
 * 
 * @author Administrator
 *    使用方法
 *   WifiManagerUtils wifiManager = new WifiManagerUtils(context);
    if (wifiManager.isConnection())
    {
      Intent intent = new Intent(context, AseoZdpUpdateService.class);
      context.startService(intent);
    }
 * 
 */
public class WifiManagerUtils {
    private WifiManager mWifiManager;
    private WifiInfo mWifiInfo;
    private List<ScanResult> mWifiList;
    private List<WifiConfiguration> mWifiConfiguration;

    public WifiManagerUtils(Context context) {
        this.mWifiManager = ((WifiManager) context.getSystemService("wifi"));

        this.mWifiInfo = this.mWifiManager.getConnectionInfo();
    }

    public boolean isConnection() {
        this.mWifiInfo = this.mWifiManager.getConnectionInfo();

        return (this.mWifiManager.isWifiEnabled()) && (this.mWifiInfo != null)
                && (this.mWifiInfo.getBSSID() != null)
                && (this.mWifiInfo.getIpAddress() != 0);
    }

    public boolean isConnection(String ssid) {
        this.mWifiInfo = this.mWifiManager.getConnectionInfo();
        if ((!this.mWifiManager.isWifiEnabled()) || (this.mWifiInfo == null))
            return false;
        String string = this.mWifiInfo.getSSID();

        return string.contains(ssid);
    }

    public int checkState() {
        return this.mWifiManager.getWifiState();
    }

    public List<WifiConfiguration> getConfiguration() {
        return this.mWifiConfiguration;
    }

    public void connectConfiguration(int index) {
        if (index > this.mWifiConfiguration.size()) {
            return;
        }

        this.mWifiManager
                .enableNetwork(
                        ((WifiConfiguration) this.mWifiConfiguration.get(index)).networkId,
                        true);
    }

    public void startScan() {
        this.mWifiManager.startScan();

        this.mWifiConfiguration = this.mWifiManager.getConfiguredNetworks();
    }

    public List<ScanResult> getWifiList() {
        this.mWifiList = this.mWifiManager.getScanResults();
        return this.mWifiList;
    }

    public StringBuilder lookUpScan() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < this.mWifiList.size(); i++) {
            stringBuilder
                    .append("Index_" + new Integer(i + 1).toString() + ":");

            stringBuilder.append(((ScanResult) this.mWifiList.get(i))
                    .toString());
            stringBuilder.append("/n");
        }
        return stringBuilder;
    }

    public String getMacAddress() {
        return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getMacAddress();
    }

    public String getBSSID() {
        return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getBSSID();
    }

    public String getSSID() {
        return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.getSSID();
    }

    public int getIPAddress() {
        return this.mWifiInfo == null ? 0 : this.mWifiInfo.getIpAddress();
    }

    public int getNetworkId() {
        return this.mWifiInfo == null ? 0 : this.mWifiInfo.getNetworkId();
    }

    public String getWifiInfo() {
        return this.mWifiInfo == null ? "NULL" : this.mWifiInfo.toString();
    }

    public void disconnectWifi(int netId) {
        this.mWifiManager.disableNetwork(netId);
        this.mWifiManager.disconnect();
    }

    public String Connect(String SSID, String Password, WifiCipherType type) {
        if (!this.mWifiManager.isWifiEnabled()) {
            return null;
        }

        this.mWifiInfo = this.mWifiManager.getConnectionInfo();
        String ssidString = getSSID();
        if ((ssidString != null) && (ssidString.contains("\""))) {
            ssidString = ssidString.split("\"")[1];
        }

        WifiConfiguration oldConfig = IsExsits(ssidString);
        if (oldConfig != null) {
            disconnectWifi(oldConfig.networkId);
        }

        WifiConfiguration tempConfig = CreateWifiInfo(SSID, Password, type);
        int tempID = this.mWifiManager.addNetwork(tempConfig);
        boolean ret = this.mWifiManager.enableNetwork(tempID, true);
        this.mWifiManager.reconnect();

        return ssidString;
    }

    public boolean RemoveWifiConfig(String ssid) {
        WifiConfiguration Config = IsExsits(ssid);
        if (Config == null) {
            return false;
        }
        this.mWifiManager.disableNetwork(Config.networkId);
        this.mWifiManager.removeNetwork(Config.networkId);
        return true;
    }

    public boolean Connect(String ssid) {
        if ((ssid != null) && (ssid.contains("\""))) {
            ssid = ssid.split("\"")[1];
        }
        WifiConfiguration tempConfig = IsExsits(ssid);
        if (tempConfig != null) {
            boolean bRet = this.mWifiManager.enableNetwork(
                    tempConfig.networkId, true);
            this.mWifiManager.reconnect();
        }
        return true;
    }

    public WifiConfiguration IsExsits(String SSID) {
        List existingConfigs = this.mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration existingConfig : existingConfigs) {
            if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
                return existingConfig;
            }
        }
        return null;
    }

    private WifiConfiguration CreateWifiInfo(String SSID, String Password,
            WifiCipherType type) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = ("\"" + SSID + "\"");

        if (type == WifiCipherType.WIFICIPHER_NOPASS) {
            config.hiddenSSID = true;
            config.allowedKeyManagement.set(0);
        } else if (type == WifiCipherType.WIFICIPHER_WPA) {
            config.preSharedKey = ("\"" + Password + "\"");
            config.hiddenSSID = true;
            config.allowedAuthAlgorithms.set(0);
            config.allowedGroupCiphers.set(2);
            config.allowedKeyManagement.set(1);
            config.allowedPairwiseCiphers.set(1);

            config.allowedGroupCiphers.set(3);
            config.allowedPairwiseCiphers.set(2);
            config.status = 2;
        } else if (type == WifiCipherType.WIFICIPHER_WEP) {
            config.hiddenSSID = true;
            config.wepKeys[0] = ("\"" + Password + "\"");
            config.allowedAuthAlgorithms.set(1);
            config.allowedGroupCiphers.set(3);
            config.allowedGroupCiphers.set(2);
            config.allowedGroupCiphers.set(0);
            config.allowedGroupCiphers.set(1);
            config.allowedKeyManagement.set(0);
            config.wepTxKeyIndex = 0;
        }
        return config;
    }

    public static enum WifiCipherType {
        WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID;
    }
}
View Code

 

Android 常见的工具类

标签:

原文地址:http://www.cnblogs.com/luoyangcn/p/4973247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!