How Android gets connected to the hotspot IP

Source: Internet
Author: User

Wifimanager Wifimanager = (wifimanager) this.getsystemservice (Context.wifi_service);
if (!wifimanager.iswifienabled ()) {
System.out.println ("=================");
Wifimanager.setwifienabled (TRUE);
}
Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();
String IPAddress = Inttoip (Wifiinfo.getipaddress ());
System.out.println ("ipaddress-->>" + IPAddress);

Dhcpinfo dhcpinfo = Wifimanager.getdhcpinfo ();
String serveraddress = Inttoip (dhcpinfo.serveraddress);
System.out.println ("serveraddress-->>" + serveraddress);
Where IPAddress is the IP address of the machine, serveraddress is the IP address of the WiFi hotspot you are connected to.

When using a WiFi hotspot on an Android device terminal, you need to know the status of the WiFi hotspot, whether the hotspot is open, the number of devices connected to the hotspot, and the specific IP and MAC addresses of the connected device.

Use the RE file manager to go to "/proc/net/arp", open, find the connection hotspot device information is here, including Mac IP and so on.

For this reason, we can open the file in code and get information about the WiFi hotspot.

Methods to get WiFi hotspot status getwifiapstate () and determine if hotspots are available isapenabled (), Implemented in Android source Wifimanager.java, but they are the Hide method, which is inaccessible at the SDK level, such as to access a mechanism that requires Java reflection. The specific code is implemented as follows:

Which defines several states of the WiFi AP

[Java]View PlainCopy  
  1. Public static final int wifi_ap_state_disabling = 10;
  2. Public static final int wifi_ap_state_disabled = 11;
  3. Public static final int wifi_ap_state_enabling = 12;
  4. Public static final int wifi_ap_state_enabled = 13;
  5. Public static final int wifi_ap_state_failed = 14;

Corresponds to the definition of these states in Wifimangaer.java.

Get the status of your WiFi hotspot:

[Java]View PlainCopy 
  1. public int Getwifiapstate (Context mcontext) {
  2. Wifimanager Wifimanager = (wifimanager) mcontext.getsystemservice (Context.wifi_service);
  3. try {
  4. method = Wifimanager.getclass (). GetMethod ("getwifiapstate");
  5. int i = (Integer) method.invoke (Wifimanager);
  6. LOG.I (TAG,"WiFi state:" + i);
  7. return i;
  8. } catch (Exception e) {
  9. LOG.E (TAG,"Cannot get WiFi AP state" + e);
  10. return wifi_ap_state_failed;
  11. }
  12. }

Determine if a WiFi hotspot is available:

[Java]View PlainCopy  
    1. Public Boolean isapenabled (Context mcontext) {
    2. int state = Getwifiapstate (Mcontext);
    3. return wifi_ap_state_enabling = = State | |    wifi_ap_state_enabled = = State;
    4. }


Get the device IP that is linked to the current hotspot:

[Java]View PlainCopy 
  1. Private Arraylist<string> Getconnectedhotip () {
  2. arraylist<string> Connectedip = new arraylist<string> ();
  3. try {
  4. BufferedReader br = new BufferedReader (new FileReader (
  5. "/proc/net/arp"));
  6. String Line;
  7. While (line = Br.readline ()) = null) {
  8. string[] splitted = Line.split ("+");
  9. if (splitted! = null && splitted.length >= 4) {
  10. String IP = splitted[0];
  11. Connectedip.add (IP);
  12. }
  13. }
  14. } catch (Exception e) {
  15. E.printstacktrace ();
  16. }
  17. return Connectedip;
  18. }
[Java]View PlainCopy 
  1. The output link to the IP address of the current device
  2. Public void Printhotip () {
  3. arraylist<string> Connectedip = Getconnectedhotip ();
  4. StringBuilder resultlist = new StringBuilder ();
  5. For (String ip:connectedip) {
  6. Resultlist.append (IP);
  7. Resultlist.append ("\ n");
  8. }
  9. System.out.print (resultlist);
  10. LOG.D (TAG,"---->>heww resultlist=" +resultlist);
  11. }

Of course, in the app to add access to the WiFi device permissions:

[HTML]View PlainCopy 
    1. <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE" />

Otherwise, you will be prompted with the following error:

Cannot get WiFi AP state

How Android gets connected to the hotspot IP

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.