简体   繁体   中英

How can I find the IP address of IP Cam on network using android programatically

I need the IP address and port no of IP cam which connected on router. You already know most of the IP Cam have admin,password and Device ID.

I know the Device ID, admin and password, I am not much familiar with java/android. If anyone share me the tutorial/blog or code snippet of java and xml with explicitly add permissions

NOTE: Assume android device and IP Cam connected on same router (local network)

NOTE: I have Chinese manufacture IP Cam. Here is the link of product. Product link

I know many App available for this purpose but my goal is to learn and doing something else.

You could ping all possible adresses to list all adresses that respond and then get their hostname to match it with your device ID.'

Code example :


import java.util.List;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.concurrent.CyclicBarrier;

public class HostFinder {
    private static int ADDR_MAX = 256;

    private List<InetAddress> reachableAdresses; 
    private String hostname;
    private CyclicBarrier cb;

    public HostFinder(String hostname){
        this.reachableAdresses = Collection.synchronizedArrayList();
    }

    public void find(Handler handler, String hostname){

        // Building possible adresses
        InetAddress localhost = InetAdress.getByName("localhost");
        String[] splitAddr = localhost.toString().split(".");
        String root = splitAddr[0] + "." + splitAddr[1] + "." + splitAddr[2];

        // Callback action when the treahd finishes
        this.cb = new CyclicBarrier(ADDR_MAX, () -> {
            for(InetAddress addr: reachableAdresses){
                if(addr.getHostName().equals(hostname)){
                    handler.handle(add);
                    break;
                }
            }
        });

        // Launching a thread that will ping each address possible on the local network
        for(int i = 0; i < ADDR_MAX; ++i) {
            Thread t = new Thread( () -> {
                InetAddress addr = InetAddress.getByName(root + "." + i);
                if(addr.isReachable(5000)){
                    reachableAdresses.add(addr);
                }
                else{
                    try {
                        this.cbr.await();
                    } catch (Exception ex) {
                        System.out.println("Thread error");
                    } 
                }
            });
            t.start();
        }
    }

    // Async handler to fetch the data
    public interface Handler{
        public void handle(InetAddress addr);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM