2

I am trying to detect the IP of a device (ESP8266), which is acting as a server and is connected to the same network. So that I can send some GET requests to it, but every time my router restarts the device get assigned a different address, so every time I have to lookup its IP from the router and put it in the app to let it start to work again.

I have found something that does my job rorist/android-network-discovery but it will be hard for me to implement it in my app (lack of documentation), secondly my antivirus detects it as a virus.

I am trying to do something like the Belking WeMo app.

What are my options now?

Jimut
  • 195
  • 3
  • 13

3 Answers3

2

there is no straight way in android where you could do this unless if you have root access, however you could do it the classic way, for example we are sure that the range of the second part range will be always between 0-255 so first thing we need to do is, getting our IP by using this method:

public static String getMyIPAddress() {//P.S there might be better way to get your IP address (NetworkInfo) could do it.
    String myIP = null;
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (isIPv4)
                        myIP = sAddr;
                }
            }
        }

    } catch (SocketException e) {
        e.printStackTrace();
    }
    return myIP;
}

method above will return the current IPthat assigned to our phone, from this we could start doing PING to indicates others who are connected to the same network, if the PING ever returned response then definitely the IP exists in the same network follow code below in a background thread:

String ipAddress = getMyIPAddress();
String subnet = ipAddress.substring(0, ipAddress.lastIndexOf("."));
String currentHost;
for (int i = 0; i < 255; i++) {
   currentHost = subnet + "." + i;
   Process p1 = Runtime.getRuntime().exec("ping -c 1 " + currentHost);
   int returnVal = p1.waitFor();
   boolean reachable = (returnVal == 0);
   if (reachable) {
       //currentHost (the IP Address) actually exists in the network
   }
}

as you can see in above code, we are looping through between 0-255 and Pining each IP Address whoever falls into reachable is actually within the network.

Kosh
  • 6,140
  • 3
  • 36
  • 67
0

Both IP Tools and Fing can do network discovery. They are free apps.

I believe they just ping every IP address in the device's subnet and wait for a response. You could make code to do this yourself.

You might want to check out this article for more info on it:

java code to ping an IP address

Tip: Use a ThreadPoolExecutor for good parallelization while waiting for the timeouts.

Community
  • 1
  • 1
whitebrow
  • 2,015
  • 21
  • 24
  • Ok, and how do I get the MAC address from the IP, I will need that for authentication purposes. – Jimut Aug 20 '15 at 15:23
  • Check this article http://stackoverflow.com/questions/8046725/get-mac-address-of-remote-pc You can't determine the MAC address of any device that makes more than one network hop, only of those that are directly connected. – whitebrow Aug 20 '15 at 15:33
0

Why wouldn't you use static IP address assignment? If you have some access restrictions to your router configuration, then you may try some other options:

  1. Remember a MAC-address of your device. Make your application on ESP8266 to request something from server and use arp -a command to find IP - MAC mapping.
  2. Write a service, that answers broadcast/multicast queries and refresh your IP-address information periodically.
  3. Develop an application that gets data from router about IP-address scope and look for you MAC there.
saver
  • 400
  • 4
  • 12