What is the API call I need to make in Android 2.2 (Froyo) to create a Wifi hotspot (as seen in the Tethering and Portable Hotspot settings item).
-
I still can't believe there is no API for activating/deactivating tethering. Hope that it exists, but is just not documented yet. – Sney Jul 02 '10 at 13:04
-
It is documented: http://www.androidjavadoc.com/2.3/android/net/wifi/WifiManager.html Just not part of the official API. – Bart Friederichs Feb 27 '13 at 20:32
3 Answers
You can call
private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);
using reflection :)
after getting the WifiManager
use the reflection to get the WifiManager
declared methods, look for this method name setWifiApEnabled
and invoke it through the WifiManager
object
These API are marked as @hide, so currently you cannot use them directly, but they appear on the AIDL for the WifiManager so their are accessible!
An example can be:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled")){
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "\"PROVAAP\"";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
try {
method.invoke(wifi, netConfig,true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
It works but I cannot change the current configuration with my own, and getting the current WifiConfiguration of an active AP drive me to an empty configuration.Why?

- 23,650
- 14
- 92
- 146

- 3,682
- 2
- 24
- 25
-
-
@markov00: Your suggested approach is not part of the Android SDK. Do not use it, period. – CommonsWare Aug 26 '10 at 03:52
-
@CommonsWare, why not? It might not work in a future version of Android, I suppose, but if you're willing to take that risk, what's wrong with that? – Tyler Aug 27 '10 at 20:06
-
2@MatrixFrog: Tactically, it may break on current devices. Device manufacturers are very willing to remove or alter this feature on behalf of carriers. Assuming that any non-SDK code will work, or even exist, is simply not reliable. If you want to use this code for some toy app you use on your own phone, be my guest. Only a fool will ship an application, though, that is designed to disappoint customers. Strategically, the more developers go past the SDK, the more difficult it is for me and others to convince manufacturers to not screw with stuff that's supposed to be supported in the SDK. – CommonsWare Aug 29 '10 at 07:41
-
5@CommonsWare: For embedded applications, it is perfectly ok to use a specific firmware and a specific device, and not publish the application on an app market. – Lars D Sep 17 '10 at 17:18
-
1I might also go with this approach (since I bundle my app with a device and am in full control of what version runs on the device). The SDK limits it's API often for no good reason - there are already many example as discussed on the Google Android dev group. Unfortunately reflection then is the only way to handle it, and I'd say go with it as long as it works for your own specific use-case. – Mathias Conradt Oct 15 '10 at 01:52
-
@markov00: is that working for your case? I tried above on SGS 2.1, but there's no method 'setWifiApEnabled' found in the loop, only found setWifiEnabled (not Ap), showApDialog, setEnabledApDialog. Another thing: How do you get/set the key? Is that in netConfig.wepKeys[0] after you invoked the method? – Mathias Conradt Oct 15 '10 at 02:47
-
-
Samsung has already implemented it in 2.1, as opposed to HTC, but it seems that the sources and methods differ. – Mathias Conradt Jan 20 '11 at 10:45
-
Is there anyway to see the implemenation of the method you are calling via reflection. For example here https://android.googlesource.com/platform/frameworks/base/+/5320e896e235564321daaa5d9f0a540cf2ca44c4%5E/wifi/java/android/net/wifi/WifiManager.java the method is not even listet. – Pascal Klein Apr 07 '13 at 18:01
-
wait,... wait... do you guys solving the case right now? I mean did the Wireless Hotspot turned on / off using this code? I didn't see where we could set it on / off from the code . @MathiasLin – gumuruh Jul 18 '14 at 09:50
-
1@PascalKlein don't know what commit you where referring to, but you can find the method in the current froyo release: https://android.googlesource.com/platform/frameworks/base/+/froyo-release/wifi/java/android/net/wifi/WifiManager.java – markov00 Jul 18 '14 at 10:18
-
@gumuruh it worked the time I tested it. It's a workaround and can work on a very limited set of device, it's not meant to be used in production. On my previous comment you can see the hidden method in WifiManager. Using java reflection as I specified in the answer, and depending on your targeting device, you can enable programmatically the Wifi AP – markov00 Jul 18 '14 at 10:20
-
seems i didnt see where i could set the On / Off from the code. But i found http://www.whitebyte.info/android/android-wifi-hotspot-manager-class#comment-34057. Looks similar. also http://omtlab.com/android-enable-disable-hotspot-programmatically/ @markov00 – gumuruh Jul 18 '14 at 10:23
-
@gumuruh the last argument of invoke method provides you the ability to turn on and off the AP ---> method.invoke(wifi, null, true); – markov00 Jul 18 '14 at 10:31
-
see... other user too doesn't realize it... just like @PascalKlein . Me too, just knew it then. Thanks again markov00 – gumuruh Jul 19 '14 at 05:26
this works on API 8 and above. I use a heavily different version then this below (or above), and was running into the same issue markov00 ran into; not being able to load the default WifiConfiguration for the portable Wi-Fi AP. I found a solution elsewhere.
If you like the solution, it would be nice if this was accepted as an answer
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for (Method method: wmMethods){
if (method.getName().equals("setWifiApEnabled")){
try {
// just nullify WifiConfiguration to load the default configuration ;)
method.invoke(wifi, null, true);
} catch (IllegalArgumentException e){
e.printStackTrace();
} catch (IllegalAccessException e){
e.printStackTrace();
} catch (InvocationTargetException e){
e.printStackTrace();
}
}
}

- 3,378
- 2
- 37
- 35
-
-
@dmmh, is it true by adding "method.invoke(wifi, null, true);" it is enabled. and so i just change the boolean values into "false" to turn it off? What about to check it's current state whether it is on / off? – gumuruh Jun 23 '14 at 04:47
There does not appear to be an API call to create a WiFi hotspot -- sorry!

- 986,068
- 189
- 2,389
- 2,491