How do I enable/disable hotspot or tethering mode programmatically on Android?

Viewed 2815

I see many references on Android's website to local Only Hotspot

However I need to manage the cellular hotspot programmatically from a background service as I can do manually from the pulldown menu.

This used to be done like:

method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
method.invoke(wifiManager, wifiConfiguration, activated);

However this feature has been deprecated.

My wireless provider (AT&T) is trying to charge me differently based on what device is connected and how. The network should be agnostic to devices and just transport my packets to their destination. I hope this is not related, but I am worried we our losing control over our devices.

Does Android really not provide simple API calls for managing the hotspot?

1 Answers

Only privileged apps can can enable/disable hotspots in Android. If you happen to be developing such an app (or have a rooted device), you can use the below APIs.

If using Android Version >= 11

You can start a hotspot with TetheringManager#startTethering code here. Here is an example from the official docs on usage.

Similarly, there is a TetheringManager#stopTethering method with code here. This will stop a hotspot.

If using Android Version < 11

ConnectivityManager#startTethering can be used with code details here. Likewise, there is a ConnectivityManager#stopTethering method with code here.

Related