3

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?

physiii
  • 209
  • 3
  • 12

1 Answers1

1

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.

Always Learning
  • 2,623
  • 3
  • 20
  • 39
  • Thank you! I am trying to test out your suggestion and had a quick question. Can I git clone https://cs.android.com/android/platform/superproject? This is so I don't have to recreate each file in my project, just import. I found a mirror on gitlab but it does not have the tethering code. Thanks again. – physiii May 04 '21 at 16:56
  • I found this: git clone https://android.googlesource.com/kernel/superproject but it does not get me to the tethering code. – physiii May 04 '21 at 17:18
  • I get `Service tethering: not found` I am using Android version 9. – physiii May 04 '21 at 20:45
  • Finally had time to test this and it looks like `startTethering` is a private method indicated by `@hide`. So I get this message `Cannot resolve method 'startTethering' in 'ConnectivityManager'` – physiii May 10 '21 at 01:15
  • I have `` in AndroidManifest but it installs as a regular app. And I don't have write access to `/etc/permissions`. Does this require me to root my device (Samsung SM-N950U)? That limits the number of people who can use the app but if it's the only way I will do it. – physiii May 11 '21 at 01:57
  • 1
    I have needed this for many different use cases - right now I need it to turn into a hotspot when I get into my car because I run Ubuntu in there (https://github.com/physiii/open-dash) and want it to autoconnect without me having to fiddle with my phone. But even if there wasn't an obvious use case, I want to explore my devices and have complete control to program as I want. Why limit us? For safety? Come on. – physiii May 15 '21 at 18:32
  • Sadly, and weirdly, the official docs example at https://source.android.com/devices/tech/connect/wifi-softap-tethering#source is gone. 404. – Anders_K Mar 24 '23 at 08:25