4

I am wondering if there is any way to get Wi-Fi hotspot status (on or off) in Android Oreo (API 26 and higher) without using reflection, because it will not work on Android P according to the new restrictions on non-sdk interfaces

I've seen that there are some methods that provide the ability to disable a WiFi hotspot using reflection, but I would like to avoid those and simply being able to know if the hotspot is enabled.

Thanks in advance!

Cris
  • 774
  • 9
  • 32
  • check this link : https://stackoverflow.com/questions/6394599/android-turn-on-off-wifi-hotspot-programmatically – Pasindu Weerakoon Jul 30 '18 at 08:27
  • thanks @wm_pasindu, but except the answer that uses reflection, which I am trying to avoid, all others are older than Oreo and therefore don't are suitable anymore – Cris Jul 30 '18 at 08:31

1 Answers1

-1

Please look at the following code. This will help you

public class WifiApManager {
    private final WifiManager mWifiManager;

    public WifiApManager(Context context) {
        mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    }

    /*the following method is for getting the wifi hotspot state*/

    public WIFI_AP_STATE getWifiApState() {
        try {
            Method method = mWifiManager.getClass().getMethod("getWifiApState");

            int tmp = ((Integer) method.invoke(mWifiManager));

            // Fix for Android 4
            if (tmp > 10) {
                tmp = tmp - 10;
            }

            return WIFI_AP_STATE.class.getEnumConstants()[tmp];
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
        }
    }

    /**
     * Return whether Wi-Fi Hotspot is enabled or disabled.
     * 
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }

}

Where WIFI_AP_STATE is an enum which is as follows

  public enum WIFI_AP_STATE {
      WIFI_AP_STATE_DISABLING, 
      WIFI_AP_STATE_DISABLED, 
      WIFI_AP_STATE_ENABLING, 
      WIFI_AP_STATE_ENABLED, 
      WIFI_AP_STATE_FAILED
  }
amit
  • 659
  • 1
  • 8
  • 21
  • Unfortunately, this is not suitable because as I stated on the question, I want to avoid using reflection, especially because Android P will introduce new restrictions on non-sdk interfaces https://developer.android.com/preview/restrictions-non-sdk-interfaces – Cris Jul 30 '18 at 09:17