如何开始使用Android手机系统的IP地址? [英] How to get the IP address of a system using Android phone?

查看:163
本文介绍了如何开始使用Android手机系统的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何开始使用Android手机的PC的IP地址? (特别是如何获取与特定MAC地址的系统,连接在同一网络上的Andr​​oid手机的IP地址)?

 有线有线
调制解调器路由器-------- --------- PC(MAC:EE:00:B3:2F:56:12)
               |
               |
               |
             无线
               |
               |
               |
               ------------- Android手机(A)


解决方案

 私人字符串getIP(字符串MAC){
  串IP = NULL;
  尝试{
    BR的BufferedReader =新的BufferedReader(新的FileReader(的/ proc /净/ ARP));
    串线=;
    而((行= br.readLine())!= NULL){
      的String []标记= line.split(\\\\ S +);
      // ARP表的形式:
      // IP地址HW型标志硬件地址掩码设备
      // 192.168.178.21为0x1 0X2 00:1A:2B:3C:4D:5E * tiwlan0
      如果(tokens.length> = 4和&放大器;令牌[3] .equalsIgnoreCase(MAC)){
        IP =令牌[0];
        打破;
      }
    }
    br.close();
  }
  赶上(例外五){Log.e(TAG,e.toString()); }
  返回的IP;
}

但要注意:除非你已经用你的电脑建立了联系(和您将需要自己的IP地址或名称),ARP表是空的。

我想你想做到这一点反过来,建立与PC只知道它的MAC地址的连接。然后,它不是那么简单。你可以尝试ping每个人都在本地网络上(调用Runtime.getRuntime()EXEC(平安-b 192.168.178.255); )正好填补了ARP表。

或者,也许,你可以从你的路由器获得他们的IP地址的所有客户端的列表?

How to get the IP address of the PC using Android phone? (In particular how to fetch the IP address of a system with a specific MAC address, connected on the same network as the Android phone)?

       wired         wired    
modem--------router---------PC(mac:EE:00:B3:2F:56:12)
               |
               |
               |
             wireless
               |
               |
               |
               -------------android phone(A)  

解决方案

private String getIP(String mac) {
  String ip = null;
  try {
    BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
    String line = "";
    while((line = br.readLine()) != null) {
      String[] tokens = line.split("\\s+");
      // The ARP table has the form:
      //   IP address        HW type    Flags     HW address           Mask   Device
      //   192.168.178.21    0x1        0x2       00:1a:2b:3c:4d:5e    *      tiwlan0
      if(tokens.length >= 4 && tokens[3].equalsIgnoreCase(mac)) {
        ip = tokens[0];
        break;
      }
    }
    br.close();
  }
  catch(Exception e) { Log.e(TAG, e.toString()); }
  return ip;
}

But beware: Unless you have already established contact with your PC (and you'll need either its IP address or its name), the ARP table will be empty.

I suppose you'd like to do it the other way round, establish the connection with the PC knowing only its MAC address. Then it's not that simple. You might try to ping everyone on the local network (Runtime.getRuntime().exec("ping -b 192.168.178.255");) just to fill the ARP table.

Or, maybe, you can get the list of all clients with their IP addresses from your router?

这篇关于如何开始使用Android手机系统的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆