How to enable Android O WiFi Hotspot programmatically

10.3k views Asked by At

We've been using the WifiManager hidden API to enable Wifi tethering and create an access point. The problem now with API 26 on Android is that the tag @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) was added to the setWifiApEnabled function and now that invocation returns false and the following log is printed:

W/WifiManager: PACKAGE_NAME attempted call to setWifiApEnabled enabled = true

Reading the new documentation, it says that the function is deprecated and will be removed in an upcoming release and to instead use ConnectivityManager#startTethering if the caller has proper permission but this approach has the same problem with no TETHER_PRIVILEGED permissions. One other option presented on the documentation is the use of LocalOnlyHotspot but this is no use to the project since the hotspot created has no Internet connectivity.

I've also tested with the startSoftAp function from the hidden API but with no successful results since the following exception is thrown:

WifiService: Neither user 10164 nor current process has android.permission.NETWORK_STACK

Is there any workaround to activate a Wifi hotspot on newer APIs? I've also searched for alternatives to Wifi tethering but the only viable alternative seems to be Bluetooth tethering with the downside of only getting 3G speed. Wifi Aware and Wifi P2P were also considered but these have the problem of the Internet connectivity not being shared between the devices.

Any suggestions on how to approach this?

2

There are 2 answers

0
Snehashish Agarwal On

No, In Android O they have increased security, So now you cannot call "startTethering" via reflection as it requires OnStartTetheringCallback which is hidden, so unless you compile a system apk with android build you can use the API. You can use LocalOnlyHotspotCallback if you want

0
Android4Fun On

This has been answered many times. As, Snehashish Agarwal said, setWifiApEnabled() and startThethering() methods are private APIs. They are annotated with @hide which means they are private. Third party applications should not be invoking private APIs directly. There is no guarantee that they will work in future. As in your case.

Also, startLocalOnlyHotspot will not have the internet access. It is a local only hotspot to communicate between devices connected to the Wifi hotspot and this method will not have Internet access.

Currently, there are no official API call for enabling the Mobile hotspot.