Android11热点启动和关闭

2024-05-24 09:44
文章标签 启动 关闭 热点 android11

本文主要是介绍Android11热点启动和关闭,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android官方关于Wi-Fi Hotspot (Soft AP) 的文章:https://source.android.com/docs/core/connect/wifi-softap?hl=zh-cn

Android 11WifiManager类中有一套系统 API 可以控制热点的开和关,代码如下:

开启热点:

val builder = SoftApConfiguration.Builder()
builder.setSsid("30666")
builder.setPassphrase("99999999", SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)
val softApConfig = builder.build()val success = wifiManager.startTetheredHotspot(softApConfig)// 或者:
val success = wifiManager.startSoftAp(softApConfig)

停止热点:

val success = wifiManager.stopSoftAp()

对于startTetheredHotspotstartSoftAp的区别,通义千问的答案如下:
在这里插入图片描述

SoftApConfiguration.BuilderstartTetheredHotspotstartSoftApstopSoftAp

这些API都被@SystemApi注解所标注,所以我们无法直接调用,只能通过反射来调用,而且这些API需要一个权限,这个权限需要系统签名的应用才可以使用,权限如下:

<uses-permission android:name="android.permission.NETWORK_STACK"/>

反射调用示例代码如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {// val builder = SoftApConfiguration.Builder() // 创建 SoftApConfiguration.Builder 对象val builderClass = Class.forName("android.net.wifi.SoftApConfiguration\$Builder")val builder = builderClass.newInstance()Log.i("ABCD","builder = $builder")// builder.setSsid("30666") 设置热点的 SSID(名称)val setSsidMethod = builderClass.getMethod("setSsid", String::class.java)setSsidMethod.invoke(builder, ssid)// builder.setPassphrase("99999999", SoftApConfiguration.SECURITY_TYPE_WPA2_PSK) 设置热点的密码val setPassphraseMethod = builderClass.getMethod("setPassphrase", String::class.java, Int::class.javaPrimitiveType)setPassphraseMethod.invoke(builder, password, SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)// builder.build() 构建 SoftApConfiguration 对象val buildMethod = builderClass.getMethod("build")val softApConfig = buildMethod.invoke(builder) as SoftApConfiguration// wifiManager.startSoftAp(softApConfig) 启动热点val startSoftApMethod = WifiManager::class.java.getMethod("startSoftAp", SoftApConfiguration::class.java)val success = startSoftApMethod.invoke(wifiManager, softApConfig) as Booleanif (success) {Toast.makeText(this, "Soft AP start successfully", Toast.LENGTH_SHORT).show()} else {Toast.makeText(this, "Failed to start Soft AP", Toast.LENGTH_SHORT).show()}
}

忘记哪个API是30才出的了,所以做了一个版本上的判断,Android R为Android 11版本。

对于启动热点,可用 WifiManager 中的 setWifiApEnabledstartSoftApstartTetheredHotspot,或者 ConnectivityManagerstartTetheredHotspot,其中,ConnectivityManagerstartTethering 只负责启动或关闭热点,对于热点名称密码等的一些配置要用WifiManagersetWifiApConfiguration函数。在ChatGPT中问这几个函数的区别时,答案如下:

  1. setWifiApEnabled

    • 作用: 启用或禁用 Wi-Fi 热点。

    • 参数:

      • wifiConfiguration: 配置热点的 WifiConfiguration 对象。
      • enabled: 一个布尔值,用于指示是否启用热点。
    • 使用情况:这是较早版本的 API(从 API 级别 8 开始)。但从 Android O(API 26)开始,此方法已被弃用,不推荐使用。

    • 示例代码:

      WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      WifiConfiguration wifiConfig = new WifiConfiguration();
      wifiConfig.SSID = "MyHotspot";
      wifiConfig.preSharedKey = "password";
      wifiManager.setWifiApEnabled(wifiConfig, true);
      
  2. startSoftAp

    • 作用: 启动一个软件热点(Soft AP)。

    • 参数:

      • softApConfiguration: 配置热点的 SoftApConfiguration 对象。
    • 使用情况:这是从 Android O(API 26)开始引入的方法,用于替代 setWifiApEnabled。这个方法更加灵活和现代化,允许更详细的热点配置。

    • 示例代码:

      WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      SoftApConfiguration.Builder builder = new SoftApConfiguration.Builder();
      builder.setSsid("MyHotspot");
      builder.setPassphrase("password", SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
      SoftApConfiguration config = builder.build();
      wifiManager.startSoftAp(config);
      
  3. startTetheredHotspot

    • 作用: 启动一个软件热点(Soft AP)。

    • 参数:

      • softApConfiguration: 配置热点的 SoftApConfiguration 对象。
    • 使用情况:

    • 示例代码:

      val builder = SoftApConfiguration.Builder()
      builder.setSsid("30666")
      builder.setPassphrase("99999999", SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)
      val softApConfig = builder.build()
      val success = wifiManager.startTetheredHotspot(softApConfig)
      

直接访问系统API

问了一下GPT,说是把自己的应用配置为系统应用即可,不只是加系统签名,还需要把应用拷贝到/system/priv-app/目录,这样手机连接Android Studio后估计就能识别你这个app在这个手机上是一个系统级app,此时可能就会直接让你访问系统API了,这有时间需要验证一下,还有如果验证是对的,然后我们再把app从/system/priv-app/中删除,然后直接运行app,看是否还能正常运行,即使调用系统API的代码报错是否还能正常运行,这也需要验证一下。

把apk转为系统应用步骤如下:

adb root // 需要有root权限
adb remount
adb push /path/to/your/app.apk /system/priv-app/YourApp.apk
adb shell chmod 644 /system/priv-app/YourApp.apk  // 设置适当的权限
adb reboot

这篇关于Android11热点启动和关闭的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/998053

相关文章

sysmain服务可以禁用吗? 电脑sysmain服务关闭后的影响与操作指南

《sysmain服务可以禁用吗?电脑sysmain服务关闭后的影响与操作指南》在Windows系统中,SysMain服务(原名Superfetch)作为一个旨在提升系统性能的关键组件,一直备受用户关... 在使用 Windows 系统时,有时候真有点像在「开盲盒」。全新安装系统后的「默认设置」,往往并不尽编

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

解决若依微服务框架启动报错的问题

《解决若依微服务框架启动报错的问题》Invalidboundstatement错误通常由MyBatis映射文件未正确加载或Nacos配置未读取导致,需检查XML的namespace与方法ID是否匹配,... 目录ruoyi-system模块报错报错详情nacos文件目录总结ruoyi-systnGLNYpe

解决hive启动时java.net.ConnectException:拒绝连接的问题

《解决hive启动时java.net.ConnectException:拒绝连接的问题》Hadoop集群连接被拒,需检查集群是否启动、关闭防火墙/SELinux、确认安全模式退出,若问题仍存,查看日志... 目录错误发生原因解决方式1.关闭防火墙2.关闭selinux3.启动集群4.检查集群是否正常启动5.

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin