Is there an API to turn On/Off the WiFi HotSpot on Android programmatically?
有没有API可以通过编程方式打开/关闭Android上的WiFi热点?
What methods should I call to turn it On/Off?
我应该调用什么方法来打开/关闭它?
UPDATE:There's this option to have the HotSpot enabled, and just turn On/Off the WiFi, but this is not a good solution for me.
更新:有这个选项可以让热点打开/关闭WiFi,但这对我来说不是一个好的解决方案。
9 个解决方案
#1
47
Use the class below to change/check the Wifi hotspot setting:
使用下面的类来更改/检查Wifi热点设置:
import android.content.*;
import android.net.wifi.*;
import java.lang.reflect.*;
public class ApManager {
//check whether wifi hotspot on or off
public static boolean isApOn(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
try {
Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifimanager);
}
catch (Throwable ignored) {}
return false;
}
// toggle wifi hotspot on or off
public static boolean configApState(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiConfiguration wificonfiguration = null;
try {
// if WiFi is on, turn it off
if(isApOn(context)) {
wifimanager.setWifiEnabled(false);
}
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wificonfiguration, !isApOn(context));
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
} // end of class
You need to add the permissions below to your AndroidMainfest:
您需要将以下权限添加到AndroidMainfest:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Use this standalone ApManager class from anywhere as follows:
从以下任何地方使用这个独立的ApManager类:
ApManager.isApOn(YourActivity.this); // check Ap state :boolean
ApManager.configApState(YourActivity.this); // change Ap state :boolean
Hope this will help someone
希望这能帮助某人
#2
8
There are no methods in the Android SDK pertaining to the WiFi hotspot feature -- sorry!
Android SDK中没有与WiFi热点特性相关的方法——抱歉!
#3
4
You can use the following code to enable, disable and query the wifi direct state programatically.
您可以使用以下代码以编程方式启用、禁用和查询wifi direct state。
package com.kusmezer.androidhelper.networking;
import java.lang.reflect.Method;
import com.google.common.base.Preconditions;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
public final class WifiApManager {
private static final int WIFI_AP_STATE_FAILED = 4;
private final WifiManager mWifiManager;
private final String TAG = "Wifi Access Manager";
private Method wifiControlMethod;
private Method wifiApConfigurationMethod;
private Method wifiApState;
public WifiApManager(Context context) throws SecurityException, NoSuchMethodException {
context = Preconditions.checkNotNull(context);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiControlMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,boolean.class);
wifiApConfigurationMethod = mWifiManager.getClass().getMethod("getWifiApConfiguration",null);
wifiApState = mWifiManager.getClass().getMethod("getWifiApState");
}
public boolean setWifiApState(WifiConfiguration config, boolean enabled) {
config = Preconditions.checkNotNull(config);
try {
if (enabled) {
mWifiManager.setWifiEnabled(!enabled);
}
return (Boolean) wifiControlMethod.invoke(mWifiManager, config, enabled);
} catch (Exception e) {
Log.e(TAG, "", e);
return false;
}
}
public WifiConfiguration getWifiApConfiguration()
{
try{
return (WifiConfiguration)wifiApConfigurationMethod.invoke(mWifiManager, null);
}
catch(Exception e)
{
return null;
}
}
public int getWifiApState() {
try {
return (Integer)wifiApState.invoke(mWifiManager);
} catch (Exception e) {
Log.e(TAG, "", e);
return WIFI_AP_STATE_FAILED;
}
}
}
#4
3
Your best bet will be looking at the WifiManager class. Specifically the setWifiEnabled(bool)
function.
你最好的选择是看一下WifiManager课程。特别是setWifiEnabled(bool)功能。
See the documentation at: http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
参见文档: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: http://www.tutorialforandroid.com/2009/10/turn-off-turn-on-wifi-in-android-using.html
关于如何使用它的教程(包括您需要的权限)可以在这里找到:http://www.tutorialforandroid.com/2009/10/turn- turn-on- wifiin android- use .html
#5
#6
1
This works well for me:
这对我来说很有效:
WifiConfiguration apConfig = null;
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
method.invoke(wifimanager, apConfig, true);
#7
1
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 8.0,有一个新的API来处理热点。据我所知,使用反射的旧方法已经不起作用了。请参考:
Android开发者https://developer.android.com/reference/android/net/wifi/WifiManager.html startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback % 20 handler)
void startLocalOnlyHotspot (WifiManager.LocalOnlyHotspotCallback callback,
Handler handler)
Request a local only hotspot that an application can use to communicate between co-located devices connected to the created WiFi hotspot. The network created by this method will not have Internet access.
请求应用程序可以用来在连接到创建的WiFi热点的共同定位设备之间进行通信的本地唯一热点。此方法创建的网络将无法访问Internet。
Stack Overflow
How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)
Stack Overflow如何在Android 8.0 (Oreo)中以编程方式打开/关闭wifi热点
onStarted(WifiManager.LocalOnlyHotspotReservation reservation) method will be called if hotspot is turned on.. Using WifiManager.LocalOnlyHotspotReservation reference you call close() method to turn off hotspot.
onstart(WifiManager。如果热点被打开,将调用LocalOnlyHotspotReservation方法。使用WifiManager。LocalOnlyHotspotReservation引用调用close()方法关闭热点。
#8
0
Applies to Oreo+ only...
只适用于奥利奥+……
I made an app with code here on GitHub which uses reflection and DexMaker to 'get at' Oreo's tethering functionality, which is now in ConnectionManager
, rather than WifiManager
.
我在GitHub上做了一个代码应用,它使用反射和DexMaker来“获取”奥利奥的连接功能,而不是WifiManager。
The stuff in WifiManager
is good only for a closed wifi network (which explained the Closed bit in the class names!).
WifiManager中的内容只适用于封闭的wifi网络(它解释了类名中关闭的部分!)
More explanation https://*.com/a/49356255/772333.
更多解释https://*.com/a/49356255/772333。
#9
-3
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
where status may be true
or false
哪里的地位可能是真或假
add permission manifest: <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
添加权限清单:
#1
47
Use the class below to change/check the Wifi hotspot setting:
使用下面的类来更改/检查Wifi热点设置:
import android.content.*;
import android.net.wifi.*;
import java.lang.reflect.*;
public class ApManager {
//check whether wifi hotspot on or off
public static boolean isApOn(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
try {
Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifimanager);
}
catch (Throwable ignored) {}
return false;
}
// toggle wifi hotspot on or off
public static boolean configApState(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiConfiguration wificonfiguration = null;
try {
// if WiFi is on, turn it off
if(isApOn(context)) {
wifimanager.setWifiEnabled(false);
}
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wificonfiguration, !isApOn(context));
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
} // end of class
You need to add the permissions below to your AndroidMainfest:
您需要将以下权限添加到AndroidMainfest:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Use this standalone ApManager class from anywhere as follows:
从以下任何地方使用这个独立的ApManager类:
ApManager.isApOn(YourActivity.this); // check Ap state :boolean
ApManager.configApState(YourActivity.this); // change Ap state :boolean
Hope this will help someone
希望这能帮助某人
#2
8
There are no methods in the Android SDK pertaining to the WiFi hotspot feature -- sorry!
Android SDK中没有与WiFi热点特性相关的方法——抱歉!
#3
4
You can use the following code to enable, disable and query the wifi direct state programatically.
您可以使用以下代码以编程方式启用、禁用和查询wifi direct state。
package com.kusmezer.androidhelper.networking;
import java.lang.reflect.Method;
import com.google.common.base.Preconditions;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
public final class WifiApManager {
private static final int WIFI_AP_STATE_FAILED = 4;
private final WifiManager mWifiManager;
private final String TAG = "Wifi Access Manager";
private Method wifiControlMethod;
private Method wifiApConfigurationMethod;
private Method wifiApState;
public WifiApManager(Context context) throws SecurityException, NoSuchMethodException {
context = Preconditions.checkNotNull(context);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiControlMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,boolean.class);
wifiApConfigurationMethod = mWifiManager.getClass().getMethod("getWifiApConfiguration",null);
wifiApState = mWifiManager.getClass().getMethod("getWifiApState");
}
public boolean setWifiApState(WifiConfiguration config, boolean enabled) {
config = Preconditions.checkNotNull(config);
try {
if (enabled) {
mWifiManager.setWifiEnabled(!enabled);
}
return (Boolean) wifiControlMethod.invoke(mWifiManager, config, enabled);
} catch (Exception e) {
Log.e(TAG, "", e);
return false;
}
}
public WifiConfiguration getWifiApConfiguration()
{
try{
return (WifiConfiguration)wifiApConfigurationMethod.invoke(mWifiManager, null);
}
catch(Exception e)
{
return null;
}
}
public int getWifiApState() {
try {
return (Integer)wifiApState.invoke(mWifiManager);
} catch (Exception e) {
Log.e(TAG, "", e);
return WIFI_AP_STATE_FAILED;
}
}
}
#4
3
Your best bet will be looking at the WifiManager class. Specifically the setWifiEnabled(bool)
function.
你最好的选择是看一下WifiManager课程。特别是setWifiEnabled(bool)功能。
See the documentation at: http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
参见文档: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: http://www.tutorialforandroid.com/2009/10/turn-off-turn-on-wifi-in-android-using.html
关于如何使用它的教程(包括您需要的权限)可以在这里找到:http://www.tutorialforandroid.com/2009/10/turn- turn-on- wifiin android- use .html
#5
2
I have publish unofficial api's for same, it's contains more than just hotspot turn on/off
. link
我也发布了非官方的api,它不仅仅包含热点打开/关闭。链接
For API's DOC - link.
用于API的文档链接。
#6
1
This works well for me:
这对我来说很有效:
WifiConfiguration apConfig = null;
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
method.invoke(wifimanager, apConfig, true);
#7
1
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 8.0,有一个新的API来处理热点。据我所知,使用反射的旧方法已经不起作用了。请参考:
Android开发者https://developer.android.com/reference/android/net/wifi/WifiManager.html startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback % 20 handler)
void startLocalOnlyHotspot (WifiManager.LocalOnlyHotspotCallback callback,
Handler handler)
Request a local only hotspot that an application can use to communicate between co-located devices connected to the created WiFi hotspot. The network created by this method will not have Internet access.
请求应用程序可以用来在连接到创建的WiFi热点的共同定位设备之间进行通信的本地唯一热点。此方法创建的网络将无法访问Internet。
Stack Overflow
How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)
Stack Overflow如何在Android 8.0 (Oreo)中以编程方式打开/关闭wifi热点
onStarted(WifiManager.LocalOnlyHotspotReservation reservation) method will be called if hotspot is turned on.. Using WifiManager.LocalOnlyHotspotReservation reference you call close() method to turn off hotspot.
onstart(WifiManager。如果热点被打开,将调用LocalOnlyHotspotReservation方法。使用WifiManager。LocalOnlyHotspotReservation引用调用close()方法关闭热点。
#8
0
Applies to Oreo+ only...
只适用于奥利奥+……
I made an app with code here on GitHub which uses reflection and DexMaker to 'get at' Oreo's tethering functionality, which is now in ConnectionManager
, rather than WifiManager
.
我在GitHub上做了一个代码应用,它使用反射和DexMaker来“获取”奥利奥的连接功能,而不是WifiManager。
The stuff in WifiManager
is good only for a closed wifi network (which explained the Closed bit in the class names!).
WifiManager中的内容只适用于封闭的wifi网络(它解释了类名中关闭的部分!)
More explanation https://*.com/a/49356255/772333.
更多解释https://*.com/a/49356255/772333。
#9
-3
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
where status may be true
or false
哪里的地位可能是真或假
add permission manifest: <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
添加权限清单: