【问题标题】:How can I find the IP address of IP Cam on network using android programatically如何以编程方式使用 android 在网络上找到 IP Cam 的 IP 地址
【发布时间】:2019-08-02 13:39:02
【问题描述】:

我需要连接在路由器上的 IP cam 的 IP 地址和端口号。您已经知道大部分网络摄像机都有管理员、密码和设备 ID。

我知道设备ID、管理员和密码,我对java/android不太熟悉。如果有人与我分享 java 和 xml 的教程/博客或代码 sn-p 并明确添加权限

注意:假设 android 设备和 IP Cam 连接在同一路由器(本地网络)上

注意:我有中国制造的网络摄像机。这是产品的链接。 Product link

我知道许多应用程序可用于此目的,但我的目标是学习和做其他事情。

【问题讨论】:

    标签: java android xml router ip-camera


    【解决方案1】:

    您可以 ping 所有可能的地址以列出所有响应的地址,然后让它们的主机名与您的设备 ID 匹配。'

    代码示例:

    
    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);
        }
    }
    
    

    【讨论】:

    • 我需要在android上以编程方式使用这个,你能分享一下代码sn-p吗?
    • 我添加了一个代码示例来异步 ping 网络上所有可能的地址并匹配它们的主机名
    • 感谢您的帮助,但我该如何执行此代码。我没有java编程基础。你能详细说明你的代码吗?所以我在我的 Eclipse IDE 中测试了这段代码。
    • 只需创建我提供给您的 IDE 的类,创建一个 main 并使用您要查找的主机名调用 find 方法
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2021-10-25
    • 2010-09-14
    • 1970-01-01
    • 2016-07-12
    • 2011-10-19
    • 2019-07-10
    相关资源
    最近更新 更多