[英]How can I find the IP address of IP Cam on network using android programatically
我需要連接在路由器上的IP cam的IP地址和端口號。 您已經知道大多數IP Cam具有admin,密碼和設備ID。
我知道設備ID,管理員和密碼,對Java / android不太熟悉。 如果有人與我分享具有顯式添加權限的Java和xml的教程/博客或代碼段
注意:假設android設備和IP Cam連接在同一路由器(本地網絡)上
注意:我有中國制造的IP Cam。 這是產品的鏈接。 產品鏈接
我知道有很多為此目的可用的應用程序,但我的目標是學習和做其他事情。
您可以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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.