How to activate the mobile hotspot on Android programmatically… Or not…

Jean Creuzé des Châtelliers
3 min readJan 25, 2018

--

Introduction

For one of my applications, I need to create a functionality which allows the user to activate and deactivate the mobile hotspot without sending him to the tethering settings but directly from the application.

First question we must ask to ourself is: is it possible? Looking at different discussions found on stackoverflow and different applications downloaded from the Play Store and test them, the answer seems to be “Yes”.

OK, so let’s see how to write a such functionality!

Development

After doing some research, I found that the solution is to use the methods of the WifiManager class.

First, we need to instantiate a new WifiManager object and a new WifiConfiguration object like below:

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "MyDummySSID";

Then we create the method which activates and deactivates the hotspot:

private void changeStateWifiAp(boolean activated) {
Method method;
try {
method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
method.invoke(wifiManager, wifiConfiguration, activated);
} catch (Exception e) {
e.printStackTrace();
}
}

You can see that we use reflection to access to the setWifiApEnabled method. It’s because the method is annotated with the @hide annotation which means it’s a private API.

Of course, we don’t forget to use the necessary permissions:

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

Now it’s time to test this functionality. I used a Samsung Galaxy S7 with Android 7 and a Sony Xperia with Android 6. After clicking on my custom button which executes this changeStateWifiAp method, the hotspot is launched and we can see the icon in the status bar.

Now what happen when we test it on a device with Android 8 Oreo? When we want to activate the mobile hotspot, nothing happens and if we check the Logcat in Android Studio, we get this error:

W/WifiManager: <MY_PACKAGE_NAME> attempted call to setWifiApEnabled enabled = true

So, let’s try some applications downloaded from the PlayStore and test them all on Android 8 Oreo: we’ve got the same problem. It seems that the setWifiApEnabled doesn’t work since the 8th version of Android anymore.

After contacting the Google Developer Support, they answer me that third party applications should not be invoking private APIs directly, they make no guarantee that calling any of these classes will work now, or that they won’t break in the future.

Effectively, when we look at the setWifiApEnabled method in the WifiManager class, the comments are perfectly clear:

This call will be deprecated and removed in an upcoming release.  It is no longer used to start WiFi Tethering.

Conclusion

In conclusion of this article, we can say that it is strongly unrecommended to implement a such functionality for the reason that Google doesn’t provide any official API call for enabling the mobile hotspot.

Generally, don’t develop an application which uses private API for the reason that we don’t know why they are privates: will they be released in the future or will they be deleted? In the second case, your application may not work anymore or won’t be compatible with a next release of Android.

To finish, that would be welcome from Google that they implement an official API for managing the tethering. Could we hope that they will do it in a next future?

--

--

Jean Creuzé des Châtelliers

Android Developer at Sylpheo, Renault-Nissan-Mitsubishi, Saint-Quentin, Picardie, France