Network application identification

Asked

Viewed 69 times

1

Hi, I’m creating a barcode reading system where I have an app on a camera device, and it sends that information to a web page on a PC. But I created to make the page to make a search in the user’s local network and find the IP of the device where the application would be. Is it possible to do this with Javascript or PHP? If so, please state the name of the technology I use to search for this subject!

  • Why not use java? Your application is hybrid?

  • The application is developed in android studio and if you are going to use Java how could do this?

1 answer

0


With android (java), you can do this:

public static String getLocalIpAddress() {
    try {
        // gera uma lista enumerada de todas as interfaces de endereços de rede
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement(); // pega o elemento
            // gera uma nova lista com todos os endereços de IP da interface
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement(); // pega o endereço IP
                // verifica se este endereço não é um "localhost" e verifica se este é um IP de protocolo  IPv4 
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                    // retorna o valor em string
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

This function returns the Ipv4 protocol IP

If you want Ipv6, you should use in your if of this foma:

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address)

Or so, to catch either one:

if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet6Address || inetAddress instanceof Inet4Address))

And then you send this information to the site. Better?

Sources:

Documentation of Android

Stackoverflow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.