Enable status detection and SSID acquisition for Android portable hotspots, and android hotspot ssid

Source: Internet
Author: User

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 ();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.