当前位置: 首页 > news >正文

android12 SDK31 wifi开发(仅提供连接wifi的工具类)

android12设备上,连接wifi

  • 说明
  • 注意:我公司的工控屏,是android12、且已经root了的系统!!!

说明

最近在android12的已经Root的设备上开发连接wifi的功能,wifi的列表很轻松的获取到了,但是连接wifi的功能一直不行,以下仅仅提供连接wifi的工具类

注意:我公司的工控屏,是android12、且已经root了的系统!!!

以下是kotlin工具类:

import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReaderobject MyWifiConnector {/*** 连接到指定 WiFi 网络(自动检测认证类型)** @param ssid WiFi 名称* @param password WiFi 密码* @return 连接是否成功*/fun connectToWifi(ssid: String, password: String): Boolean {return try {// 1. 扫描网络并获取认证类型val authType = detectAuthType(ssid) ?: run {// 如果无法检测到认证类型,尝试常见类型return tryConnectWithFallback(ssid, password)}// 2. 使用检测到的认证类型连接executeConnectCommand(ssid, authType, password)} catch (e: Exception) {e.printStackTrace()false}}/*** 扫描 WiFi 网络并检测指定 SSID 的认证类型*/private fun detectAuthType(targetSsid: String): String? {val process = Runtime.getRuntime().exec("su")val outputStream = DataOutputStream(process.outputStream)// 执行扫描命令outputStream.writeBytes("cmd wifi list-scan-results\n")outputStream.writeBytes("exit\n")outputStream.flush()// 读取扫描结果val reader = BufferedReader(InputStreamReader(process.inputStream))val response = StringBuilder()var line: String?while (reader.readLine().also { line = it } != null) {response.append(line).append("\n")}process.waitFor()outputStream.close()// 解析扫描结果,查找目标 SSIDval lines = response.toString().split("\n")for (line in lines) {if (line.contains(targetSsid)) {// 解析认证类型return when {line.contains("WPA3", ignoreCase = true) -> "wpa3"line.contains("WPA2", ignoreCase = true) -> "wpa2"line.contains("WPA", ignoreCase = true) -> "wpa"line.contains("WEP", ignoreCase = true) -> "wep"line.contains("ESS", ignoreCase = true) && !line.contains("PSK",ignoreCase = true) -> "open"else -> null}}}return null}/*** 尝试使用常见认证类型连接*/private fun tryConnectWithFallback(ssid: String, password: String): Boolean {// 尝试常见认证类型val authTypes = listOf("wpa2", "wpa", "wep", "open")for (authType in authTypes) {if (executeConnectCommand(ssid, authType, password)) {return true}// 等待片刻再尝试下一种类型Thread.sleep(1000)}return false}/*** 执行实际的连接命令*/private fun executeConnectCommand(ssid: String, authType: String, password: String): Boolean {return try {val process = Runtime.getRuntime().exec("su")val outputStream = DataOutputStream(process.outputStream)// 构建连接命令val command = "cmd wifi connect-network \"$ssid\" $authType \"$password\"\n"outputStream.write(command.toByteArray())outputStream.writeBytes("exit\n")outputStream.flush()// 读取输出val reader = BufferedReader(InputStreamReader(process.inputStream))val response = StringBuilder()var line: String?while (reader.readLine().also { line = it } != null) {response.append(line).append("\n")}process.waitFor()outputStream.close()// 检查连接结果response.toString().contains("Connection initiated", ignoreCase = true)} catch (e: Exception) {false}}
}
http://www.dtcms.com/a/389599.html

相关文章:

  • Android播放视频适配黑边问题类型总结
  • 第十一章:AI进阶之--模块的概念与使用(二)
  • 异常检测patchcore 学习笔记 2025
  • [iOS] 网络 - AFNetWorking
  • iOS App 混淆与性能稳定性优化 混淆开销、崩溃风险、CI 集成与落地实务(
  • Freertos系统(任务挂起和恢复)
  • Git更新仓库时,忽略指定文件
  • 告别“瞎练”!数据闭环正成机器人智能进化核心引擎!
  • 基于MATLAB的无人机遥感数据预处理与农林植被性状估算
  • MATLAB基于AHP-模糊综合评价法的工程实践能力评价
  • 特征选择+优化算法+GBDT+SHAP分析!ReliefF-CPO-GBDT分类预测结合SHAP可解释分析MATLAB
  • 设计模式-外观模式详解
  • 《FastAPI零基础入门与进阶实战》第19篇:消息管理
  • 类和对象(下):static成员、友元类、内部类、匿名对象、优化——对象拷贝时的编译器优化
  • 虚拟线程(Virtual Thread)
  • 1688 店铺全商品接口技术全解:从页面解析到分页采集的完整实现方案
  • 日志轮转策略针对香港VPS存储空间的设置标准
  • 线性分组码及其相关概念
  • JWT的工作流程
  • Java 25 新特性 更简洁、更高效、更现代
  • 探讨前端与后端的安全策略:保护用户数据的关键措施
  • 如何使用DeepSeek等AI工具来帮助自己的工作
  • 灵途科技亮相CIOE2025 | 光电感知赋能具身智能升级
  • 我的云端影院:LibreTV+cpolar的异地观影记
  • NW748NW765美光固态闪存NW775NW781
  • 软考中级习题与解答——第八章_计算机网络(1)
  • Playwright 完全指南:从入门到实战,解锁自动化测试新范式
  • OpenCV:直接用NV21/NV12格式,画线、贴图都是相加效果,而不是替换、覆盖
  • MCP3421与STM32电压采集实现
  • 表白网页制作免费网站制作 表白网站建设教程