How can Java find out that the phone is in the "access point" mode?

I need to find out if the phone distributes Wi-Fi / is connected to it.

Here is the code to check the connection to Wi-Fi:

public boolean isWiFiOn() { final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); if(wifi != null) switch(wifi.getWifiState()) { case(WifiManager.WIFI_STATE_DISABLED): case(WifiManager.WIFI_STATE_DISABLING): return false; default: return true; } return false; } 

But this code returns false if the phone is in access point mode.

How can I find out if he is distributing Wi-Fi?

    1 answer 1

    Officially, you can not find out, but Android is on the "android", that a lot of things can be done with the help of hacks:
    There is a hidden @hide method:

     Method method = wifiManager.getClass().getDeclaredMethod("getWifiApState"); method.setAccessible(true); int actualState = (Integer) method.invoke(wifiManager, (Object[]) null); 

    Further, the actualState compared with such constants:

     public static int WIFI_AP_STATE_DISABLING = 10; //выключается public static int WIFI_AP_STATE_DISABLED = 11; //выключен public static int WIFI_AP_STATE_ENABLING = 12; //включается public static int WIFI_AP_STATE_ENABLED = 13; //включен public static int WIFI_AP_STATE_FAILED = 14; //сломалсо 

    UPD: reference to the method in the source:

    • Thank. And constants in what class? - Maxim Drobyshev
    • Decided to use another method: isWifiApEnabled. Actually the same thing, but he checks himself. Thanks again. - Maxim Drobyshev
    • @MaksimDrobyshev, constants in the same class, but they are hidden, and you have to copy them anyway to your class. - Vladyslav Matviienko
    • one
      @MaksimDrobyshev, there is a chance that isWifiApEnabled will show the wrong result, since it checks the status only on AP_STATE_ENABLED, and does not check it is turned on and broke , and this may become a problem - Vladyslav Matviienko
    • only constants still start with WIFI_AP_STATE - Maxim Drobyshev