Android Portable hotspot Open State detection and SSID acquisition

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 16px">How does the WiFi hotspot open and the SSID after it is opened?</span></p></p><p><p><span style="font-size: 16px"></span></p></p><p><p><span style="font-size: 16px">Open the Wifimanager.java source code, you can find the Getwifiapstate () method, the surprise found that the direct call to this method can get to the state of the hotspot, however, when the call is not called to this method ... This method is hidden, and I am currently calling through Reflection.</span></p></p><p><p></p></p><pre><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* Gets the Wi-fi enabled State. * </span></span><span style="color: #808080"><span style="color: #808080">@return</span></span><span style="color: #008000">One of <span style="color: #008000">{</span></span><span style="color: #808080"><span style="color: #808080">@link</span></span><span style="color: #008000"><span style="color: #008000">#WIFI_AP_STATE_DISABLED}, * {</span></span><span style="color: #808080"><span style="color: #808080">@link</span></span><span style="color: #008000"><span style="color: #008000">#WIFI_AP_STATE_DISABLING}, {</span></span><span style="color: #808080"><span style="color: #808080">@link</span></span><span style="color: #008000"><span style="color: #008000">#WIFI_AP_STATE_ENABLED}, * {</span></span><span style="color: #808080"><span style="color: #808080">@link</span></span><span style="color: #008000"><span style="color: #008000">#WIFI_AP_STATE_ENABLING}, {</span></span><span style="color: #808080"><span style="color: #808080">@link</span></span><span style="color: #008000"><span style="color: #008000">#WIFI_AP_STATE_FAILED} *</span></span><span style="color: #808080"><span style="color: #808080">@see</span></span><span style="color: #008000"><span style="color: #008000">#isWifiApEnabled () * * @hide Dont Open Yet</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span> <span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">int</span></span><span style="color: #000000"><span style="color: #000000">getwifiapstate () {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Try</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span><span style="color: #000000"><span style="color: #000000">mservice.getwifiapenabledstate (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(remoteexception E) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span><span style="color: #000000"><span style="color: #000000">wifi_ap_state_failed; } }</span></span></pre><p><p></p></p><p><p><span style="font-size: 16px">So I wrote a radiation to get the state of the hot Spot.</span></p></p><p><p></p></p><pre> <span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">Boolean</span></span><span style="color: #000000"><span style="color: #000000">Iswifiapopen (context Context) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Try</span></span><span style="color: #000000"><span style="color: #000000">{wifimanager Manager</span></span>=<span style="color: #000000"><span style="color: #000000">(wifimanager) Context.getsystemservice (context.wifi_service); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">obtaining the Getwifiapstate () method by radiation</span></span>method = Manager.getclass (). Getdeclaredmethod ("getwifiapstate"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">call Getwifiapstate () to get the return value</span></span> <span style="color: #0000ff"><span style="color: #0000ff">int</span></span>State = (<span style="color: #0000ff"><span style="color: #0000ff">int</span></span><span style="color: #000000"><span style="color: #000000">) Method.invoke (manager); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">get the open state property of Wifi_ap by radiation</span></span>Field field = Manager.getclass (). Getdeclaredfield ("wifi_ap_state_enabled"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Get Property value</span></span> <span style="color: #0000ff"><span style="color: #0000ff">int</span></span>Value = (<span style="color: #0000ff"><span style="color: #0000ff">int</span></span><span style="color: #000000"><span style="color: #000000">) Field.get (manager); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">decide whether to open</span></span> <span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(state = =<span style="color: #000000"><span style="color: #000000">Value) { </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span> <span style="color: #0000ff"><span style="color: #0000ff">true</span></span><span style="color: #000000"><span style="color: #000000">; } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Else</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span> <span style="color: #0000ff"><span style="color: #0000ff">false</span></span><span style="color: #000000"><span style="color: #000000">; } } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(nosuchmethodexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(illegalaccessexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(invocationtargetexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(nosuchfieldexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span> <span style="color: #0000ff"><span style="color: #0000ff">false</span></span><span style="color: #000000"><span style="color: #000000">; }</span></span></pre><p><p></p></p><p><p><span style="font-size: 16px">Using the Getwifiapstate () method to return the value of the comment, you can find the following states, get the current status value, only need to compare the values of various states, you know the open state of the hotspot</span></p></p><pre><pre> * @<span style="color: #0000ff">return</span> one<span style="color: #000000"> of {@link #WIFI_STATE_DISABLED}, </span>*<span style="color: #000000"> {@link #WIFI_STATE_DISABLING}, {@link # wifi_state_enabled}, </span>* {@link #WIFI_STATE_ENABLING}, {@link #WIFI_STATE_UNKNOWN}</pre></pre><p><p></p></p><p><p><span style="font-size: 16px">similarly, the SSID that gets to the hotspot through reflection</span></p></p><p><p><span style="font-size: 16px"></span></p></p><pre> <span style="color: #0000ff"><span style="color: #0000ff">Try</span></span><span style="color: #000000"><span style="color: #000000">{wifimanager Manager</span></span>= (wifimanager)<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">. Getsystemservice (context.wifi_service); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Get the Getwifiapconfiguration () method</span></span>method = Manager.getclass (). Getdeclaredmethod ("getwifiapconfiguration"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Call the Getwifiapconfiguration () method to get the wificonfiguration to the hotspot</span></span>Wificonfiguration configuration =<span style="color: #000000"><span style="color: #000000">(wificonfiguration) Method.invoke (manager); SSID</span></span>=<span style="color: #000000"><span style="color: #000000">Configuration. SSID; } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(nosuchmethodexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(invocationtargetexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff"><span style="color: #0000ff">Catch</span></span><span style="color: #000000"><span style="color: #000000">(illegalaccessexception E) {e.printstacktrace (); }</span></span></pre><p><p></p></p><p><p>Android Portable hotspot Open State detection and SSID acquisition</p></p></span>
Related Article

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.