Android system information retrieval 14: Getting WIFI Hotspot information
When using a Wi-Fi hotspot on an Android device, you need to know the running status of the Wi-Fi hotspot, whether the hotspot is enabled, and the number of devices connected to the Wifi hotspot, and the specific IP address and MAC address of the connected device.
Use the re File Manager to go to "/proc/net/arp", open it, and find that all the device information on the connection hotspot is here, including the mac ip address.
In view of this, we can open the file in the code and get the information about the WIFI hotspot.
The getWifiApState () method for obtaining the WIFI hotspot status and the isApEnabled () method for determining whether the hotspot is available, in the Android source code WifiManager. java has been implemented, but they are Hide methods and cannot be accessed at the SDK level. java reflection is required for access. The Code is as follows:
Several statuses of the Wi-Fi AP are defined.
public static final int WIFI_AP_STATE_DISABLING = 10; public static final int WIFI_AP_STATE_DISABLED = 11; public static final int WIFI_AP_STATE_ENABLING = 12; public static final int WIFI_AP_STATE_ENABLED = 13; public static final int WIFI_AP_STATE_FAILED = 14;
Corresponding to the definition of these States in WifiMangaer. java.
Obtain the status of a Wi-Fi hotspot:
public int getWifiApState(Context mContext) { WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); try { Method method = wifiManager.getClass().getMethod("getWifiApState"); int i = (Integer) method.invoke(wifiManager); Log.i(TAG,"wifi state: " + i); return i; } catch (Exception e) { Log.e(TAG,"Cannot get WiFi AP state" + e); return WIFI_AP_STATE_FAILED; } }
Determine whether Wifi hotspots are available:
public boolean isApEnabled(Context mContext) { int state = getWifiApState(mContext); return WIFI_AP_STATE_ENABLING == state || WIFI_AP_STATE_ENABLED == state; }
Obtain the IP address of the device linked to the current hotspot:
private ArrayList<String> getConnectedHotIP() {ArrayList<String> connectedIP = new ArrayList<String>();try {BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));String line;while ((line = br.readLine()) != null) {String[] splitted = line.split(" +");if (splitted != null && splitted.length >= 4) {String ip = splitted[0];connectedIP.add(ip);}}} catch (Exception e) {e.printStackTrace();}return connectedIP;}
// Output the public void printHotIp () {ArrayList <String> connectedIP = getConnectedHotIP (); StringBuilder resultList = new StringBuilder (); for (String ip: connectedIP) to the current device) {resultList. append (ip); resultList. append ("\ n");} System. out. print (resultList); Log. d (TAG, "----> heww resultList =" + resultList );}
Of course, you must add the permission to access the WIFI device in the application:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Otherwise, the following error will be prompted:
Cannot get WiFi AP state
----------------------------------
Welcome to browse, technical exchange, respect for labor achievements, repost, please indicate the source, thank you!
Http://blog.csdn.net/netwalk/article/details/23183501