WifiHelper.java 8.16 KB
Newer Older
pye52 committed
1
package com.bgycc.smartcanteen.utils;
patpat committed
2 3 4 5 6 7 8 9

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

pye52 committed
10 11 12
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;

13
import java.net.NetworkInterface;
patpat committed
14 15
import java.util.List;

pye52 committed
16
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
patpat committed
17

pye52 committed
18 19
@SuppressWarnings("all")
public class WifiHelper {
patpat committed
20

pye52 committed
21 22
    private static WifiManager getWifiManager() {
        return (WifiManager) Utils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
patpat committed
23 24 25 26
    }

    public static DhcpInfo getDhcpInfo() {
        try {
pye52 committed
27
            return getWifiManager().getDhcpInfo();
patpat committed
28
        } catch (Exception e) {
pye52 committed
29
            LogUtils.w(TAG, "获取Dhcp信息异常: " + e.getMessage(), e);
patpat committed
30 31 32 33 34 35
        }
        return null;
    }

    public static WifiInfo getWifiInfo() {
        try {
pye52 committed
36
            return getWifiManager().getConnectionInfo();
patpat committed
37
        } catch (Exception e) {
pye52 committed
38
            LogUtils.w(TAG, "获取Wifi信息异常: " + e.getMessage(), e);
patpat committed
39 40 41 42
        }
        return null;
    }

43 44 45 46
    public static String getMacAddress() {
        try {
            NetworkInterface networkInterface = NetworkInterface.getByName("wlan0");
            byte[] data = networkInterface.getHardwareAddress();
patpat committed
47
            return String.format("%02x:%02x:%02x:%02x:%02x:%02x", data[0], data[1], data[2], data[3], data[4], data[5]);
48
        } catch (Exception e) {
pye52 committed
49
            LogUtils.w(TAG, "获取MAC地址异常: " + e.getMessage(), e);
50 51 52 53
        }
        return "02:00:00:00:00:00";
    }

patpat committed
54
    public static int getWifiNetworkID() {
pye52 committed
55 56 57 58
        WifiInfo wifiInfo = getWifiInfo();
        if (wifiInfo == null) {
            return 0;
        }
patpat committed
59
        try {
pye52 committed
60
            return wifiInfo.getNetworkId();
patpat committed
61
        } catch (Exception e) {
pye52 committed
62
            LogUtils.w(TAG, "获取Wifi Id异常: " + e.getMessage(), e);
patpat committed
63 64 65 66 67 68
        }
        return 0;
    }

    public static boolean setEnable(boolean enable) {
        try {
69
            if (isWifiEnabled()) return true;
pye52 committed
70
            return getWifiManager().setWifiEnabled(enable);
patpat committed
71
        } catch (Exception e) {
pye52 committed
72
            LogUtils.w(TAG, "Wifi启动异常: " + e.getMessage(), e);
patpat committed
73 74 75 76 77 78
            return false;
        }
    }

    public static boolean isWifiEnabled() {
        try {
pye52 committed
79
            return getWifiManager().isWifiEnabled();
patpat committed
80
        } catch (Exception e) {
pye52 committed
81
            LogUtils.w(TAG, "Wifi状态检查异常: " + e.getMessage(), e);
patpat committed
82 83 84 85
        }
        return false;
    }

patpat committed
86
    public static String getSSID() {
pye52 committed
87 88 89 90
        WifiInfo wifiInfo = getWifiInfo();
        if (wifiInfo == null) {
            return "02:00:00:00:00:00";
        }
patpat committed
91
        try {
pye52 committed
92
            return wifiInfo.getSSID();
patpat committed
93
        } catch (Exception e) {
pye52 committed
94
            LogUtils.w(TAG, "获取Wifi SSID异常: " + e.getMessage(), e);
patpat committed
95 96 97 98 99
            return "02:00:00:00:00:00";
        }
    }

    public static String getIpString() {
pye52 committed
100 101 102 103
        WifiInfo wifiInfo = getWifiInfo();
        if (wifiInfo == null) {
            return "0.0.0.0";
        }
patpat committed
104
        try {
pye52 committed
105 106 107 108 109
            int ip = wifiInfo.getIpAddress();
            return (ip & 0xFF) + "." +
                    ((ip >> 8) & 0xFF) + "." +
                    ((ip >> 16) & 0xFF) + "." +
                    ((ip >> 24) & 0xFF);
patpat committed
110
        } catch (Exception e) {
pye52 committed
111
            LogUtils.w(TAG, "获取Wifi IP地址异常: " + e.getMessage(), e);
patpat committed
112 113 114 115
            return "0.0.0.0";
        }
    }

patpat committed
116 117
    public static WifiConfiguration getWifiConfiguration(String ssid) {
        try {
pye52 committed
118
            List<WifiConfiguration> existingConfigs = getWifiManager().getConfiguredNetworks();
patpat committed
119 120 121 122 123 124
            for (WifiConfiguration existingConfig : existingConfigs) {
                if (existingConfig.SSID.equals("\"" + ssid + "\"")) {
                    return existingConfig;
                }
            }
        } catch(Exception e) {
pye52 committed
125
            LogUtils.w(TAG, "获取Wifi配置异常: " + e.getMessage(), e);
patpat committed
126 127 128 129 130 131 132 133
        }
        return null;
    }

    public static boolean removeWifiConfiguration(String ssid) {
        try {
            WifiConfiguration wifiConfig = getWifiConfiguration(ssid);
            if(wifiConfig != null) {
pye52 committed
134
                return getWifiManager().removeNetwork(wifiConfig.networkId);
patpat committed
135 136
            }
        } catch (Exception e) {
pye52 committed
137
            LogUtils.w(TAG, "移除Wifi配置异常: " + e.getMessage(), e);
patpat committed
138 139 140 141 142 143 144
        }
        return false;
    }
    protected static WifiConfiguration createWifiConfiguration(String ssid, String pwd, String type) {
        return createWifiConfiguration(ssid, null, pwd, type);
    }

145
    public static WifiConfiguration createWifiConfiguration(String ssid, String identity, String pwd, String type) {
patpat committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        if(ssid == null || ssid.isEmpty())
            return null;
        if (pwd != null && !pwd.isEmpty()) {
            if (type == null || type.isEmpty())
                type = "wpa";
        }
        else {
            type = null;
        }

        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = "\"" + ssid + "\"";

pye52 committed
164
        if (type == null) {
patpat committed
165
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
pye52 committed
166
        } else if ("wep".compareToIgnoreCase(type) == 0) {
patpat committed
167
            config.hiddenSSID = true;
pye52 committed
168
            config.wepKeys[0] = "\"" + pwd + "\"";
patpat committed
169 170 171 172 173 174 175
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
pye52 committed
176
        } else if ("wpa".compareToIgnoreCase(type) == 0) {
patpat committed
177 178 179 180 181 182 183 184 185
            config.preSharedKey = "\"" + pwd + "\"";
            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;
pye52 committed
186
        } else if ("wpa_eap".compareToIgnoreCase(type) == 0) {
patpat committed
187 188 189 190 191 192 193
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
            WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
            enterpriseConfig.setIdentity(identity);
            enterpriseConfig.setPassword(pwd);
            enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
            config.enterpriseConfig = enterpriseConfig;
pye52 committed
194
        } else {
patpat committed
195 196 197 198 199
            return null;
        }
        return config;
    }

200
    public static int addNetwork(WifiConfiguration config) {
pye52 committed
201
        return getWifiManager().addNetwork(config);
202 203 204
    }

    public static boolean enableNetwork(int netId) {
pye52 committed
205
        return getWifiManager().enableNetwork(netId, true);
206 207
    }

patpat committed
208 209
    public static boolean connect(String ssid, String identity, String pwd, String type) {
        WifiConfiguration config = createWifiConfiguration(ssid, identity, pwd, type);
pye52 committed
210
        if (config == null) {
patpat committed
211
            return false;
pye52 committed
212
        }
patpat committed
213

pye52 committed
214
        if (!isWifiEnabled()) {
patpat committed
215
            return false;
pye52 committed
216
        }
patpat committed
217

pye52 committed
218
        WifiManager wifiManager = getWifiManager();
patpat committed
219
        boolean ret = removeWifiConfiguration(ssid);
pye52 committed
220 221
        int netId = wifiManager.addNetwork(config);
        if (!wifiManager.enableNetwork(netId, true)) {
patpat committed
222
            return false;
pye52 committed
223
        }
patpat committed
224

pye52 committed
225
        wifiManager.reconnect();
patpat committed
226 227 228
        return true;
    }
}