Android 以编程方式打开/关闭 WiFi 热点
是否有 API 可以以编程方式打开/关闭 Android 上的 WiFi 热点?
我应该调用什么方法来打开/关闭它?
更新:有这个选项可以启用热点,然后只需打开/关闭 WiFi,但这对我来说不是一个好的解决方案。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
警告此方法将无法在 5.0 以上工作,这是一个相当过时的条目。
使用下面的类来更改/检查
Wifi 热点
设置:您需要将以下权限添加到您的 AndroidMainfest:
从任何地方使用这个独立的 ApManager 类,如下所示:
希望这会对某人有所帮助
Warning This method will not work beyond 5.0, it was a quite dated entry.
Use the class below to change/check the
Wifi hotspot
setting:You need to add the permissions below to your AndroidMainfest:
Use this standalone ApManager class from anywhere as follows:
Hope this will help someone
如果您想在 Android 应用程序中以编程方式实现 wifi 热点功能,这里是完整的解决方案。
API解决方案 26:
对于设备< API 26。Android 没有用于此目的的公共 API。因此,为了使用这些 API,您必须通过反射访问私有 API。不推荐这样做,但如果您没有其他选择,那么这里有一个技巧。
首先,您需要在清单中拥有此权限,
以下是您如何在运行时请求它:
然后您可以通过反射访问
setWifiEnabled
方法。如果您请求的操作正在正确处理,即启用/禁用热点,则此返回 true。您还可以通过反射获取您的热点的wifi配置。我已经在 StackOverflow 上回答了这个问题的方法。
PS:如果您不想以编程方式打开热点,您可以启动此 intent 并打开 wifi设置屏幕供用户手动打开它。
API >= 26 的解决方案:
最后,android 为 >= Oreo 版本发布了官方 API。您可以使用android ie startLocalOnlyHotspot
它打开没有互联网访问的本地热点。因此可以用来托管服务器或传输文件
需要
Manifest.permission.CHANGE_WIFI_STATE
和ACCESS_FINE_LOCATION
权限以下是如何使用此 API 打开热点的方法
。 :
以下是获取本地创建的热点详细信息的方法
。如果即使在授予所需权限后仍抛出安全异常,那么您应该尝试使用 GPS 启用您的位置。 href="https://stackoverflow.com/a/57367371/10936389">解决方案
最近,我开发了一个名为 Spotserve。这将为所有具有 API>=15 的设备打开 WiFi 热点,并在该热点上托管一个演示服务器。您可以检查以获取更多详细信息。希望这有帮助!
Here's the complete solution if you want to implement the wifi hotspot feature programmatically in your android app.
SOLUTION FOR API < 26:
For devices < API 26. There is no public API by Android for this purpose. So, in order to work with those APIs you've to access private APIs through reflection. It is not recommended but if you've no other options left, then here's a trick.
First of all, you need to have this permission in your manifest,
Here's how you can ask it on run-time:
You can then access the
setWifiEnabled
method through reflection. This returns true if the action you asked for is being process correctly i.e. enabling/disabling hotspot.You can also get the wificonfiguration of your hotspot through reflection. I've answered that method for this question on StackOverflow.
P.S: If you don't want to turn on hotspot programmatically, you can start this intent and open the wifi settings screen for user to turn it on manually.
SOLUTION FOR API >= 26:
Finally, android released an official API for versions >= Oreo. You can just use the public exposed API by android i.e. startLocalOnlyHotspot
It turns on a local hotspot without internet access. Which thus can be used to host a server or transfer files.
It requires
Manifest.permission.CHANGE_WIFI_STATE
andACCESS_FINE_LOCATION
permissions.Here's a simple example of how you can turn on hotspot using this API.
The method to turn on hotspot:
Here's how you can get details of the locally created hotspot
If it throws, a security exception even after giving the required permissions then you should try enabling your location using GPS. Here's the solution.
Recently, I've developed a demo app called Spotserve. That turns on wifi hotspot for all devices with API>=15 and hosts a demo server on that hotspot. You can check that for more details. Hope this helps!
对于 Android 8.0,有一个新的 API 来处理热点。据我所知,使用反射的旧方法不再有效。
请参考:
Android开发者
https://developer.android.com/reference/android/net/wifi/WifiManager.html#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,%20android.os.Handler)
堆栈溢出
如何在 Android 8.0 (Oreo) 中以编程方式打开/关闭 wifi 热点
For Android 8.0, there is a new API to handle Hotspots. As far as I know, the old way using reflection doesn't work anymore.
Please refer to:
Android Developers
https://developer.android.com/reference/android/net/wifi/WifiManager.html#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,%20android.os.Handler)
Stack Overflow
How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)
警告 此方法在 5.0 之后不起作用,这是一个相当过时的条目。
您可以使用以下代码以编程方式启用、禁用和查询 wifi direct 状态。
Warning This method will not work beyond 5.0, it was a quite dated entry.
You can use the following code to enable, disable and query the wifi direct state programatically.
这对我来说效果很好:
This works well for me:
APManager - 接入点管理器
Step 1 : Add the jcenter repository to your build file
Step 2 : Add the dependency
Step 3 use in your app
查看源代码
https://github.com/vijaypatidar/AndroidWifiManager
APManager - Access Point Manager
Step 1 : Add the jcenter repository to your build file
Step 2 : Add the dependency
Step 3 use in your app
check out source code
https://github.com/vijaypatidar/AndroidWifiManager
我已经发布了相同的非官方 api,它不仅仅包含热点打开/关闭。 链接
API 文档 - 链接。
I have publish unofficial api's for same, it's contains more than just hotspot turn
on/off
. linkFor API's DOC - link.
您最好的选择是查看 WifiManager 类。特别是
setWifiEnabled(bool)
函数。请参阅以下位置的文档:
http://developer.android.com/参考/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
有关如何使用它的教程(包括您需要什么权限)可以在这里找到:
https://web.archive.org/web/20210228070142/http://www.tutorialforandroid.com/2009/10/turn-off-turn-on-wifi-in-android-using.html
Your best bet will be looking at the WifiManager class. Specifically the
setWifiEnabled(bool)
function.See the documentation at:
http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
A tutorial on how to use it (including what permissions you need) can be found here:
https://web.archive.org/web/20210228070142/http://www.tutorialforandroid.com/2009/10/turn-off-turn-on-wifi-in-android-using.html
**对于奥利奥和PIE ** 我发现了以下方式通过此
UseAge
**For Oreo & PIE ** I found below way through this
UseAge
我们可以通过编程方式打开和关闭
使用回调类,以编程方式打开pie(9.0)中的热点,您需要以编程方式关闭并打开。
We can programmatically turn on and off
Using callback class, to programmatically turn on hotspot in pie(9.0) u need to turn off programmatically and the switch on.
您可以使用该选项的控制台和服务。
我想用这个你可以解决这个问题。我直接在控制台中进行了测试并启用了
我在这篇文章中找到的 热点
是否可以USB tether 通过终端使用 adb 的 Android 设备?
读取接口后我们可以使用相同的 24 但需要更多参数
You can use the console and service for that option.
I think with this you can solve that. I made a test directly in console and enable hotspot
I found this in this article
Is it possible to USB tether an android device using adb through the terminal?
And after read the interface we can use the same 24 but needs more parameters
您可以使用具有 root 访问权限的命令
行来打开热点。
关闭热点
You can use command line with root access
To turn on hotspot.
To turn off hotspot
其中状态可能为
true
或false
添加权限清单:
where status may be
true
orfalse
add permission manifest:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />