SoFunction
Updated on 2025-04-10

How to detect the on-state of Android portable hotspot and obtain SSID

How to obtain the on-state of WIFI hotspot and the SSID after opening?

Open the source code and find the getWifiApState() method. If you are surprised to find that you can directly call this method to get the status of the hot spot. However, this method cannot be called when calling. . . This method is hidden, and I am currently calling it through reflection.

/**
   * Gets the Wi-Fi enabled state.
   * @return One of {@link #WIFI_AP_STATE_DISABLED},
   *     {@link #WIFI_AP_STATE_DISABLING}, {@link #WIFI_AP_STATE_ENABLED},
   *     {@link #WIFI_AP_STATE_ENABLING}, {@link #WIFI_AP_STATE_FAILED}
   * @see #isWifiApEnabled()
   *
   * @hide Dont open yet
   */
  public int getWifiApState() {
    try {
      return ();
    } catch (RemoteException e) {
      return WIFI_AP_STATE_FAILED;
    }
  }

So I wrote about a radiation to obtain the status of the hot spot

 public static boolean isWifiApOpen(Context context) {
    try {
      WifiManager manager = (WifiManager) (Context.WIFI_SERVICE);
      //Get getWifiApState() method through radiation      Method method = ().getDeclaredMethod("getWifiApState");
      //Call getWifiApState() to get the return value      int state = (int) (manager);
      //Get the on state attribute of WIFI_AP through radiation      Field field = ().getDeclaredField("WIFI_AP_STATE_ENABLED");
      //Get the attribute value      int value = (int) (manager);
      //Judge whether it is turned on      if (state == value) {
        return true;
      } else {
        return false;
      }
    } catch (NoSuchMethodException e) {
      ();
    } catch (IllegalAccessException e) {
      ();
    } catch (InvocationTargetException e) {
      ();
    } catch (NoSuchFieldException e) {
      ();
    }
    return false;
  }

Through the comment of the return value of the getWifiApState() method, you can find the following states. After getting the current state value, you only need to compare the values ​​of various states to know the opening state of the hotspot.

* @return One of {@link #WIFI_STATE_DISABLED},
   *     {@link #WIFI_STATE_DISABLING}, {@link #WIFI_STATE_ENABLED},
   *     {@link #WIFI_STATE_ENABLING}, {@link #WIFI_STATE_UNKNOWN}

Similarly, the SSID of the hotspot is also obtained through reflection

  try {
            WifiManager manager = (WifiManager) (Context.WIFI_SERVICE);
            //Get the getWifiApConfiguration() method            Method method = ().getDeclaredMethod("getWifiApConfiguration");
            //Call getWifiApConfiguration() method to get the hotspot WifiConfiguration            WifiConfiguration configuration = (WifiConfiguration) (manager);
            ssid = ;
          } catch (NoSuchMethodException e) {
            ();
          } catch (InvocationTargetException e) {
            ();
          } catch (IllegalAccessException e) {
            ();
          }

The above is the method of enabling status detection of Android portable hotspots and obtaining SSIDs that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!