0

I want to control wifi hotspot dynamically in my Android app project. I have tired Reflection (which will not work in Android Oreo and later versions), startLocalOnyNetwork (but I want specific SSID and PASSWORD, which is not possible to configure it).

Then I rooted my phone, Is it possible if the device is rooted ?

Expecting an api to turn on/off wifi hotspot with specific SSID and PASSWORD or use the previous one.

Any possibilities or workarounds ?

Thanks in advance.

Binil
  • 434
  • 3
  • 11
  • 1
    i don't think its possible as it should be left to the user. you may direct the user to turn on wifi by directing the user to setting screen. Note: there may be workarounds and i am not aware of any – Raghunandan Dec 01 '19 at 09:28

1 Answers1

0

To turn on Wifi Hotspot, need some permissions

<uses-permission android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" />

and the permission should be dynamically granted by user

In apps advanced settings -> Modify system settings

In apps advanced settings -> Modify system settings

/**
 * This enables tethering using the ssid/password defined in Settings App>Hotspot & tethering
 * Does not require app to have system/privileged access
 * Credit: Vishal Sharma - https://stackoverflow.com/a/52219887
 */
public boolean startTethering() {
    File outputDir = mContext.getCodeCacheDir();
    Object proxy;
    try {
        proxy = ProxyBuilder.forClass(OnStartTetheringCallbackClass())
                .dexCache(outputDir).handler(new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        return null;
                    }

                }).build();
    } catch (Exception e) {
        Log.e(TAG, "Error in enableTethering ProxyBuilder");
        e.printStackTrace();
        return false;
    }

    Method method = null;
    try {
        method = mConnectivityManager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, OnStartTetheringCallbackClass(), Handler.class);
        if (method == null) {
            Log.e(TAG, "startTetheringMethod is null");
        } else {
            method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE, false, proxy, null);
            Log.d(TAG, "startTethering invoked");
        }
        return true;
    } catch (Exception e) {
        Log.e(TAG, "Error in enableTethering");
        e.printStackTrace();
    }
    return false;
}

public void stopTethering() {
    try {
        Method method = mConnectivityManager.getClass().getDeclaredMethod("stopTethering", int.class);
        if (method == null) {
            Log.e(TAG, "stopTetheringMethod is null");
        } else {
            method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE);
            Log.d(TAG, "stopTethering invoked");
        }
    } catch (Exception e) {
        Log.e(TAG, "stopTethering error: " + e.toString());
        e.printStackTrace();
    }
}

Use above methods to turn on/off Wifi Hotspot with SSID and password defined in the settings.

private int AP_STATE_DISABLED = 11;
private int AP_STATE_ENABLING = 12;
private int AP_STATE_ENABLED = 13;
private int AP_STATE_ERROR = 14;

/**
 * @return status hot spot enabled or not
 */
public boolean isHotSpotEnabled(Context context) {
    Method method = null;
    int actualState = 0;
    try {
        WifiManager mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        method = mWifiManager.getClass().getDeclaredMethod("getWifiApState");
        method.setAccessible(true);
        actualState = (Integer) method.invoke(mWifiManager, (Object[]) null);
        if (actualState == AP_STATE_ENABLING ||actualState ==  AP_STATE_ENABLED) {
            return true;
        }
    } catch (IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

Above method can be used to get the current state of hotspot

Binil
  • 434
  • 3
  • 11