Enable status detection and SSID acquisition for Android portable hotspots, and android hotspot ssid
How can I obtain the enabling status of a Wi-Fi hotspot and the SSID after it is enabled?
Open WifiManager. java source code. You can find the getWifiApState () method. If you are surprised, you can call this method directly to obtain the hotspot state. However, you cannot call this method when calling it... This method is hidden. Currently, it is called 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 mService.getWifiApEnabledState(); } catch (RemoteException e) { return WIFI_AP_STATE_FAILED; } }
So I wrote a radiation program to get the hotspot status.
Public static boolean isWifiApOpen (Context context) {try {WifiManager manager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); // obtain the getWifiApState () Method method = manager through radiation. getClass (). getDeclaredMethod ("getWifiApState"); // call getWifiApState () to obtain the return value int state = (int) method. invoke (manager); // obtain the Enabled state attribute Field = manager of WIFI_AP through radiation. getClass (). getDeclaredField ("WIFI_AP_STATE_ENABLED"); // get the attribute value int value = (int) field. get (manager); // determine whether to enable if (state = value) {return true;} else {return false;} catch (NoSuchMethodException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (InvocationTargetException e) {e. printStackTrace ();} catch (NoSuchFieldException e) {e. printStackTrace ();} return false ;}
Through the comments returned by the getWifiApState () method, you can find the following States. After obtaining the current state value, you only need to compare the values of various States to know the hot spot enabling status.
* @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 obtained through reflection.
Try {WifiManager manager = (WifiManager) this. getSystemService (Context. WIFI_SERVICE); // get the getWifiApConfiguration () Method method = manager. getClass (). getDeclaredMethod ("getWifiApConfiguration"); // call the getWifiApConfiguration () method to obtain the WifiConfiguration configuration = (WifiConfiguration) method of the hotspot. invoke (manager); ssid = configuration. SSID;} catch (NoSuchMethodException e) {e. printStackTrace ();} catch (InvocationTargetException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();}