Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Android hotspot settings

With the release of API level 26, my app's core functionality broke, this being, changing the users' hotspot setting within the application. To get and set this configuration I am using the following functions from the WifiManager hidden api: getWifiApConfiguration and setWifiApConfiguration.

Method getWifiApConfiguration = wifiManager.getClass().getMethod("getWifiApConfiguration");
getWifiApConfiguration.invoke(wifiManager);

This is working with devices prior to Android O, but in this version I get the following error:

App not allowed to read or update stored WiFi Ap config (uid = 10168)

The permissions I have declared in the manifest are:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.NETWORK_STACK"/>
<uses-permission android:name="android.permission.TETHER_PRIVILEGED" />

How can I do this with the latest APIs?

like image 736
Tiago Ferreira Avatar asked Sep 24 '17 16:09

Tiago Ferreira


People also ask

How do I set up a hotspot on Android?

Here’s how you configure a hotspot connection on Android: Open the Settings app. Select the Network & Internet option. Select Hotspot & tethering.

How to change hotspot name on Android device?

To change the hotspot name on your Android device, follow these steps. Step 2: Locate the hotspot icon and long press it. Step 3: Portable hotspot settings open up. Now, select Set up portable hotspot Step 4: in the SSID field, you will find the current name of your mobile hotspot.

How to enable 5GHz WiFi hotspot on Android devices?

Tap “Hotspot & Tethering” and select “Advanced”. Scroll to AP Band then select the 5GHz frequency. Restart your phone afterwards. Go to One Android setting and select “Wi-Fi & Internet” option. Scroll to Hotspot & Tethering and tap Wi-Fi hotspot. Switch the current speed to 5GHz Band.

How do I change the Wi-Fi hotspot speed of my Device?

Scroll to Hotspot & Tethering and tap Wi-Fi hotspot. Switch the current speed to 5GHz Band. After restarting your One device, check the connected devices to check if the internet speed has increased.


1 Answers

As of Android Oreo (26), a new permission check was added to the service implementation of the getWifiApConfiguration() method:

Snippet from WifiServiceImpl.java

/**
 * see {@link WifiManager#getWifiApConfiguration()}
 * @return soft access point configuration
 * @throws SecurityException if the caller does not have permission to retrieve the softap
 * config
 */
@Override
public WifiConfiguration getWifiApConfiguration() {
    enforceAccessPermission();
    int uid = Binder.getCallingUid();
    // only allow Settings UI to get the saved SoftApConfig
    if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) {
        // random apps should not be allowed to read the user specified config
        throw new SecurityException("App not allowed to read or update stored WiFi Ap config "
                + "(uid = " + uid + ")");
    }
    mLog.trace("getWifiApConfiguration uid=%").c(uid).flush();
    return mWifiStateMachine.syncGetWifiApConfiguration();
}

Digging into the code you will quickly find out that to successfully invoke this method your application must hold the android.permission.OVERRIDE_WIFI_CONFIG permission that is a system level protected permission:

Snippet from framework AndroidManifest.xml

<!-- @SystemApi @hide Allows an application to modify any wifi configuration, even if created
 by another application. Once reconfigured the original creator cannot make any further
 modifications.
 <p>Not for use by third-party applications. -->
<permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"
    android:protectionLevel="signature|privileged" />

This means that your application needs to be signed by the platform key or be privileged to use this API.

like image 57
David Lev Avatar answered Sep 30 '22 07:09

David Lev