Here is a way to get the IP connected to the hotspot in Android. The specific code is as follows:
WifiManager wifiManager = (WifiManager) (Context.WIFI_SERVICE); if (!()) { ("================="); (true); } WifiInfo wifiInfo = (); String IPAddress = intToIp(()); ("IPAddress-->>" + IPAddress); DhcpInfo dhcpinfo = (); String serverAddress = intToIp(); ("serverAddress-->>" + serverAddress);
Among them, IPAddress is the IP address of the local machine, and serverAddress is the IP address corresponding to the wifi hotspot you are connected to.
private String intToIp(int paramInt) { return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "." + (0xFF & paramInt >> 24); }
When using a Wifi hotspot on an Android device terminal, you need to know the operating status of the Wifi hotspot, whether the hotspot is on, the number of devices connected to the WIFI hotspot, and the specific IP and MAC address of the connected device.
Use the re file manager to go"/proc/net/arp
", open it and find that the device information on the hotspot is here, including mac ip, etc.
In view of this, we can open the file in the code and get information about the WIFI hotspot.
The method to getWifiApState() and the method to determine whether the hotspot is available areApEnabled(). It has been implemented in the Android source code, but they are Hide methods and cannot be accessed at the SDK level. If you want to access, you need to use Java reflection mechanism. The specific code is implemented as follows:
Several states of WIFI 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.
Get the status of WIFI hotspot:
public int getWifiApState(Context mContext) { WifiManager wifiManager = (WifiManager) (Context.WIFI_SERVICE); try { Method method = ().getMethod("getWifiApState"); int i = (Integer) (wifiManager); (TAG,"wifi state: " + i); return i; } catch (Exception 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; }
Get the device IP 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 = ()) != null) { String[] splitted = (" +"); if (splitted != null && >= 4) { String ip = splitted[0]; (ip); } } } catch (Exception e) { (); } return connectedIP; } //Output the IP address of the link to the current devicepublic void printHotIp() { ArrayList<String> connectedIP = getConnectedHotIP(); StringBuilder resultList = new StringBuilder(); for (String ip : connectedIP) { (ip); ("\n"); } (resultList); (TAG,"---->>heww resultList="+resultList); }
Of course, you need to add permissions to access WIFI devices in the application:
<uses-permission android:name=".ACCESS_WIFI_STATE" />
Otherwise, the following error will be prompted:
Cannot get WiFi AP state
Summarize
The above is the method of getting the Android IP connected to hotspots introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!