Android Hotspot Related actions

Source: Internet
Author: User

Android does not provide direct access to the API, need to use reflection, the code is simpler, as follows

Gethotspotstate.java
Package club.seliote.hotspotscanner.utils; Import Android.content.context;import Android.net.wifi.WifiManager; Import Java.lang.reflect.Method; /** * Used to get hotspot status */public class Gethotspotstate {/** * get hotspot Status * @return Hotspot_state enumeration * @throws exceptio N, may be globalapplicationcontext or thrown from reflection */public static hotspot_state GetState () throws Exception {//need to use GE  Tapplicationcontext (), otherwise an error//Error:the Wifi_service must be looked-on the application context or memory will        Leak on devices < Android N. Try changing to. Getapplicationcontext () [Wifimanagerleak] Wifimanager Wifimanager = (wifimanager) GlobalAppli          Cationcontext.getcontext (). Getapplicationcontext (). Getsystemservice (Context.wifi_service);        Direct invocation is not allowed and requires the use of reflection method = Wifimanager.getclass (). GetMethod ("Getwifiapstate");         int statecode = (Integer) method.invoke (Wifimanager); Part of the API version may be problematic, here to deal with IF (Statecode >) {statecode-= 10;    } return HOTSPOT_STATE.class.getEnumConstants () [Statecode]; }/** * Get hotspot on Status * @return turned on returns TRUE, otherwise false * @throws Exception, any step error will be thrown Exception */public Static Boolean Ishotsoptopen () throws Exception {return gethotspotstate.getstate () = = Hotspot_state.    wifi_ap_state_enabled; } }
Hotspot_state.java
package club.seliote.hotspotscanner.utils; /** * 热点状态的枚举 */public enum HOTSPOT_STATE {     // 正在关闭    WIFI_AP_STATE_DISABLING,     // 已关闭    WIFI_AP_STATE_DISABLED,     // 正在开启    WIFI_AP_STATE_ENABLING,     // 已开启    WIFI_AP_STATE_ENABLED,     //错误状态    WIFI_AP_STATE_FAILED }

Next is to obtain the relevant connection status, the Hotspot connection state is saved in the file/proc/net/arp, if WiFi is turned on, the file will be stored in the WiFi ARP information, so be sure to check the Hotspot connection status before getting the hotspot open state, code to get a shell direct cat No, it is not, where HW type is the hardware types (hardware type of the address from RFC 826), flags refers to the internal ARP structure, when the device connection value is 0x2, the disconnect value is 0x0, thus the Hotspot connection device can be monitored

Android Hotspot Related actions

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.