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() {@Overridepublic void onSuccess() {Log.i(TAG, "forgetNetwork: onSuccess: is ok");manager.removeNetwork(networkId);}@Overridepublic 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() {@Overridepublic void onSuccess() {Log.i(TAG, "connectWifi: onSuccess: connect ok");}@Overridepublic 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) {// 设置身份验证类型为 EAPwifiConfig.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<>();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/70070.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Jmeter 三种提取方式 —— 关联实例

当请求之间有依赖关系&#xff0c;比如一个请求的入参是另一个请求返回的数据&#xff0c;这时候就需要用到关联处理 Jmeter中常用的关联方法&#xff1a; 正则表达式提取器、XPath提取器、JSON提取器​​​​​​ regex: (.*?)-(.*?)-(.*?)\n.: 匹配除换行符&#xff08;…

C# 获取Json对象中指定属性的值

在C#中获取JSON对象中指定属性的值&#xff0c;可以使用Newtonsoft.JSON库的JObject类 using Newtonsoft.Json.Linq; using System; public class Program { public static void Main(string[] args) { string json "{ Name: John, age: 30, City: New York }"; …

python实现对excel表中的某列数据进行排序

如下需要对webCms中的B列数据进行升序排序&#xff0c;且不能影响到其他列、工作表中的数据和格式。 import pandas as pd import openpyxl from openpyxl.utils.dataframe import dataframe_to_rows# 读取 Excel 文件 file_path 1.xlsx sheet_name webCms# 读取 Excel 文件并…

pc-签字画板vue-esign的使用

使用的是vue-esign组件 npm install vue-esign 首先下载组件在main.js中引入vue-esign&#xff0c;并且挂载 import { createApp } from vue; import App from ./App.vue; const app createApp(App);import vueEsign from vue-esign app.use(vueEsign ) 页面使用&#xff0…

mysql场景题:最近7天连续3天登陆用户,字段,id,date(已去重)

1.最近7天连续3天登陆用户&#xff0c;字段&#xff0c;id&#xff0c;date&#xff08;已去重&#xff09; 思路&#xff1a; lag对时间开窗&#xff08;注意时间得转换为时间戳&#xff08;int类型才可以添加后续条件&#xff09;&#xff0c;跳行为2&#xff08;连续3天&am…

uniapp移动端地图,点击气泡弹窗并实现精准定位

记录移动端地图map组件的使用 需求记录&#xff1a; 移动端地图部分需要展示两个定位点&#xff0c;上报点及人员定位点。通过右上角的两个按钮实现地图定位。点击对应定位气泡&#xff0c;弹出定位点的信息。 效果图如下&#xff1a; map在nvue中的使用。直接用nvue可以直接…

JavaScript闭包漏洞与修补措施

请先看下面一段代码 var obj (function () {var sonObj {a: 1,b: 2}return {get: function (v) {return sonObj[v]}}})()可以看出,这是一段很典型的js闭包代码,可以通过obj调用get方法传一个参数,如果传的是a就可以得到闭包内的对象sonObj.a var obj (function () {var sonO…

edge浏览器无法登录账号!Microsoft 帐户无法登录!

种种原因&#xff0c;将笔记本重置了&#xff0c;重新下载装了系统&#xff0c;但是麻烦也来了&#xff0c;Microsoft 帐户无法登录&#xff01;edge浏览器无法登录账号&#xff0c;之前的保存的密码&#xff0c;加星的书签页同步不过去&#xff0c;这不完犊子了&#xff01;干…

Redis7入门概述

✅作者简介&#xff1a;大家好&#xff0c;我是Leo&#xff0c;热爱Java后端开发者&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;Leo的博客 &#x1f49e;当前专栏&#xff1a; Java从入门到精通 ✨特色专栏&#xf…

微信小程序引入地图

微信小程序引入地图 <map longitude"{{markers[0].longitude}}" scale"11" latitude"{{markers[0].latitude}}" markers"{{markers}}" style"width: 100%; height:81vh;"></map>1.可以直接在页面设置map标签显…

谈谈你的未来吧(励志成为CV算法工程师的第一天)

谈谈你的未来吧&#xff08;励志成为CV算法工程师的第一天&#xff09; 前言一、不知道该怎么选择的大一&#xff0c;大二二&#xff0c;好像知道未来路的大三三&#xff0c;谈谈博主我吧四&#xff0c;朝着一个方向前进吧 前言 仅以此篇记录我的学习经过&#xff0c;大家有什…

解决WebSocket通信:前端拿不到最后一条数据的问题

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

服务器上装conda

服务器从0搭建-【anaconda3cudacudnnconda环境创建修改conda源】_bashrc中的conda initialize和cuda_冲上云霄&#xff01;的博客-CSDN博客

C# 子类如何访问子类的方法(同一父类)

在继承关系中&#xff0c;子类可以通过创建另一个子类的对象来访问其方法。下面是一个示例&#xff0c;展示了子类如何访问另一个子类的方法&#xff1a; public class Animal {public virtual void Speak(){Console.WriteLine("我是动物。");} }public class Cat :…

CentOS7设置虚拟内存

1、 查看服务器内存 > free -mhtotal used free shared buff/cache available Mem: 3.7G 1.4G 128M 64M 2.1G 1.9G Swap: 0 0 02、创建虚拟内存 合理规划和设计 Linux…

python学习之【深拷贝】

#我的编程语言学习笔记# 前言 上一篇文章python学习之【浅拷贝】 学习了python中的浅拷贝相关内容&#xff0c;这篇文章接着学习深拷贝。 简单回顾 浅拷贝只拷贝浅层元素&#xff0c;深层元素的内存地址不改变 &#xff1b;当对拷贝产生的新的对象的浅层元素进行更改时&…

vue2踩坑之项目:生成二维码使用vue-print-nb打印二维码

1. vue2安装 npm install vue-print-nb --save vue3安装 npm install vue3-print-nb --save 2. //vue2 引入方式 全局 main.js import Print from vue-print-nb Vue.use(Print) ------------------------------------------------------------------------------------ //vue2 …

【亲测】mwget安装成功

微信公众号&#xff1a;leetcode_algos_life&#xff0c;代码随想随记 小红书&#xff1a;412408155 CSDN&#xff1a;https://blog.csdn.net/woai8339?typeblog 抖音【暂未开始&#xff0c;计划开始】&#xff1a;tian72530 知乎【暂未开始&#xff0c;计划开始】&#xff1a…

FLUX查询InfluxDB -- InfluxDB笔记三

1. 入门 from(bucket: "example_query") // 没有筛选条件直接查询会报错|> range(start: -1h) // |>是管道符&#xff0c;后跟筛选条件 2. 序列、表和表流 序列是InfluxDB的概念&#xff0c;一个序列是由measurement、标签集、一个字段名称 表流是FLUX为了…

Matlab信号处理1:模拟去除信号噪声

由于工作内容涉及信号系统、信号处理相关知识&#xff0c;本人本硕均为计算机相关专业&#xff0c;专业、研究方向均未涉及信号相关知识&#xff0c;因此需进行系统地学习。之前已将《信号与系统》快速过了一遍&#xff0c;但感觉较抽象且理解较浅显。在此系统地学习如何使用Ma…