• Android WIFI工具类 特别兼容Android12


    直接上代码:

    package com.realtop.commonutils.utils;
    
    import android.annotation.SuppressLint;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothManager;
    import android.bluetooth.BluetoothProfile;
    import android.content.Context;
    import android.net.IpConfiguration;
    import android.net.wifi.WifiConfiguration;
    import android.net.wifi.WifiEnterpriseConfig;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Build;
    import android.text.TextUtils;
    import android.util.Log;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class WIFIHelper {
        public static final String TAG = "wifi_utils";
    
        public static String getForgetName(WifiManager manager) {
            WifiInfo connectionInfo = manager.getConnectionInfo();
            String ssid = connectionInfo.getSSID().replace("\"", "");
            return ssid;
        }
    
        public static void forgetNetwork(WifiManager manager) {
            try {
                WifiInfo connectionInfo = manager.getConnectionInfo();
                String ssid = connectionInfo.getSSID().replace("\"", "");
                int networkId = connectionInfo.getNetworkId();
                Log.i(TAG, "forgetNetwork: enter:" + ssid + "; " + networkId);
                manager.forget(networkId, new WifiManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.i(TAG, "forgetNetwork: onSuccess: is ok");
                        manager.removeNetwork(networkId);
                    }
    
                    @Override
                    public void onFailure(int reason) {
                        Log.i(TAG, "forgetNetwork: onFailure: reason:" + reason);
                        manager.removeNetwork(networkId);
                    }
                });
                ComUtils.saveString(ComConfig.WIFI_PWD_PRE + ssid, "");
                Log.i(TAG, "forgetNetwork: remove id:" + networkId + ";" + ssid);
            } catch (Exception e) {
                e.printStackTrace();
                Log.i(TAG, "forgetNetwork: error:" + e.getMessage());
            }
        }
    
        /**
         * 连接wifi
         *
         * @param manager       WifiManager
         * @param configuration Wifi配置
         * @return 是否连接成功
         */
        public static boolean connectWifi(WifiManager manager, WifiConfiguration configuration) {
            Log.i(TAG, "connectWifi: enter");
            manager.connect(configuration, new WifiManager.ActionListener() {
                @Override
                public void onSuccess() {
                    Log.i(TAG, "connectWifi: onSuccess: connect ok");
                }
    
                @Override
                public void onFailure(int reason) {
                    Log.i(TAG, "connectWifi: onFailure: error:" + reason);
                }
            });
            return true;
        }
    
        /**
         * 创建Wifi配置
         *
         * @param SSID         wifi名称
         * @param password     wifi密码
         * @param hidden       网络是否隐藏(该方法与添加隐藏网络通用)
         * @param capabilities 网络安全协议
         * @return 配置好的wifi
         */
        public static WifiConfiguration createWifiInfo(String SSID, String password, boolean hidden, String capabilities) {
            WifiConfiguration configuration = new WifiConfiguration();
            configuration.SSID = "\"" + SSID + "\"";
            if (hidden) {
                configuration.hiddenSSID = true;
            }
            Log.d("WifiManagerUtils", "createWifiInfo: " + capabilities);
            if (capabilities.contains("SAE") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                setWPA3(configuration, password);
            } else if (capabilities.contains("WPA-PSK") || capabilities.contains("WPA2-PSK")) {
                setWPA(configuration, password);
            } else if (capabilities.contains("WEP")) {
                setWEP(configuration, password);
            } else {
                setESS(configuration);
            }
            return configuration;
        }
    
        @SuppressLint("NewApi")
        public static WifiConfiguration createWifiConfiguration(String SSID, String password, boolean hidden, String capabilities) {
            WifiConfiguration config = new WifiConfiguration();
            config.SSID = "\"" + SSID + "\"";
            config.hiddenSSID = hidden;
            config.shared = false;
            Log.d("WifiManagerUtils", "createWifiInfo: " + capabilities);
    
            // 分别适配安全类型
            if (capabilities.contains("SAE")) {
                config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
                if (!TextUtils.isEmpty(password)) {
                    config.preSharedKey = '"' + password + '"';
                }
                Log.i(TAG, "createWifiConfiguration: sae");
            } else if (capabilities.contains("WPA-PSK") || capabilities.contains("WPA2-PSK")) {
                config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
                if (!TextUtils.isEmpty(password)) {
                    if (password.matches("[0-9A-Fa-f]{64}")) {
                        config.preSharedKey = password;
                    } else {
                        config.preSharedKey = '"' + password + '"';
                    }
                }
                Log.i(TAG, "createWifiConfiguration: psk");
            } else if (capabilities.contains("WEP")) {
                config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
                if (!TextUtils.isEmpty(password)) {
                    int length = password.length();
                    // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
                    if ((length == 10 || length == 26 || length == 58)
                            && password.matches("[0-9A-Fa-f]*")) {
                        config.wepKeys[0] = password;
                    } else {
                        config.wepKeys[0] = '"' + password + '"';
                    }
                }
                Log.i(TAG, "createWifiConfiguration: wep");
            } else if (capabilities.contains("OWE")) {
                config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE);
                Log.i(TAG, "createWifiConfiguration: owe");
            } else if (capabilities.contains("EAP")) {
                setEAP(config, password);
                Log.i(TAG, "createWifiConfiguration: eap");
            } else {
                // 无密码
                config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
                config.shared = true;
                Log.i(TAG, "createWifiConfiguration: open");
            }
    
    
            final IpConfiguration ipConfig = new IpConfiguration();
            ipConfig.setIpAssignment(IpConfiguration.IpAssignment.UNASSIGNED);
            ipConfig.setProxySettings(IpConfiguration.ProxySettings.UNASSIGNED);
            ipConfig.setStaticIpConfiguration(null);
            ipConfig.setHttpProxy(null);
            config.setIpConfiguration(ipConfig);
            return config;
        }
    
        private static void setEAP(WifiConfiguration wifiConfig, String password) {
            // 设置身份验证类型为 EAP
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    
            // 设置 EAP 方法(这是具体的 EAP 身份验证类型)
            wifiConfig.enterpriseConfig.setEapMethod(-1);
    
            // 设置身份验证用户名和密码
            wifiConfig.enterpriseConfig.setIdentity("");
            wifiConfig.enterpriseConfig.setPassword(password);
        }
    
        /**
         * 设置wpa3协议
         *
         * @param configuration 配置
         * @param password      密码
         */
        public static void setWPA3(WifiConfiguration configuration, String password) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.SAE);
            }
            configuration.preSharedKey = "\"" + password + "\"";
        }
    
        /**
         * WPA协议
         *
         * @param configuration 配置
         * @param password      密码
         */
        public static void setWPA(WifiConfiguration configuration, String password) {
            configuration.preSharedKey = "\"" + password + "\"";
            //公认的IEEE 802.11验证算法。
            configuration.allowedAuthAlgorithms.clear();
            configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            //公认的的公共组密码。
            configuration.allowedGroupCiphers.clear();
            configuration.allowedGroupCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            //公认的密钥管理方案。
            configuration.allowedKeyManagement.clear();
            configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            //密码为WPA。
            configuration.allowedPairwiseCiphers.clear();
            configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            //公认的安全协议。
            configuration.allowedProtocols.clear();
            configuration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        }
    
        /**
         * WEP协议
         *
         * @param configuration 配置
         * @param password      密码
         */
        public static void setWEP(WifiConfiguration configuration, String password) {
            configuration.wepKeys[0] = "\"" + password + "\"";
            configuration.wepTxKeyIndex = 0;
            configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        }
    
    
        /**
         * 无密码
         *
         * @param configuration 配置
         */
        public static void setESS(WifiConfiguration configuration) {
            configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }
    
        public static List<BluetoothDevice> getBlueConnectDevices() {
            BluetoothManager bluetoothManager = (BluetoothManager) EventBusHelper.getContext()
                    .getSystemService(Context.BLUETOOTH_SERVICE);
            @SuppressLint("MissingPermission")
            List<BluetoothDevice> connectedDevices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
            if (connectedDevices != null) {
                return connectedDevices;
            } else {
                return new ArrayList<>();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
  • 相关阅读:
    2023-10-27 LeetCode每日一题(切割后面积最大的蛋糕)
    MySQL常见锁探究
    编译器优化:何为SLP矢量化
    在verdi波形中显示状态机名字
    内存卡中毒了格式化能解决吗?这样清除病毒更有效
    20220608-SCRFD-样本和计算重分配
    python运行hhblits二进制命令的包装器类
    Linux编程知识之GLIB的GOption接口函数
    9个li+CSS实现七夕表白爱心动画&&1个h1+CSS实现跑马灯动画表白文字
    Java中的栈(Stack)为什么要采用先进后出
  • 原文地址:https://blog.csdn.net/mhhyoucom/article/details/132687994