如何使用笔记本电脑mac扫描我的网络并在路由器中找到我的笔记本电脑ip地址?

时间:2015-09-17 23:01:27

标签: java android-studio

我的笔记本电脑连接到个人电脑室的路由器。 现在每次我关闭我的笔记本电脑并再次连接它我得到一个新的IP地址。 在我的Android设备中,我想使用笔记本电脑的IP地址,但我需要每次使用我的电脑登录到路由器设置,并找到笔记本电脑的IP地址。

我想要做的是扫描网络中连接到路由器网络的所有ip的所有ip,然后找到适合笔记本电脑mac地址的ip地址,这样我知道这是我的笔记本电脑ip地址。< / p>

我有这两种方法,但我不知道如何使用第一个refreshArp:

private static void refereshArp(Context ctx){
    //IP aquisition from http://stackoverflow.com/a/6071963/1896516
    WifiManager wm = (WifiManager) ctx.getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    String[] parts = ip.split(".");
    String subnet = parts[0] + "." + parts[1] + "." + parts[2] + ".";
    Runtime rt = Runtime.getRuntime();
    for(int i=0; i<256; i++){
        try{
            rt.exec("ping -c1 " + subnet + i)
        }catch(IOException e){
            continue;
        }
    }

}

refreshArp应该得到什么?

第二种方法似乎更容易用于提供笔记本电脑的mac地址:

//Adapted from http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/
private static String getIpFromMac(String mac) {
    if (mac == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && mac.equals(splitted[3])) {
                // return the ip of the device
                return splitted[0];
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

另一个问题是从哪里调用这两种方法?来自MainActivity中的onCreate?

我正在使用我的java代码android studio 1.3.2

0 个答案:

没有答案
相关问题