Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android上获取本地网络中所有设备的IP地址和名称_Java_Android_Networking_Lan - Fatal编程技术网

Java 如何在Android上获取本地网络中所有设备的IP地址和名称

Java 如何在Android上获取本地网络中所有设备的IP地址和名称,java,android,networking,lan,Java,Android,Networking,Lan,我想用java查看网络上所有连接的设备,但我无法让它工作。我在下面附上了一些我希望它如何输出的截图。我想知道名称(例如“TP链路路由器”或“Nexus 5X”)和IP地址 我在谷歌和stackoverflow上搜索了很多,但似乎什么都不适合我。甚至GitHub也没有有效的代码。我试着搜索UPnP、局域网、子网等,但什么也没找到 InetAddress localhost = InetAddress.getLocalHost(); byte[] ip = localhost.getAddress(

我想用java查看网络上所有连接的设备,但我无法让它工作。我在下面附上了一些我希望它如何输出的截图。我想知道名称(例如“TP链路路由器”或“Nexus 5X”)和IP地址

我在谷歌和stackoverflow上搜索了很多,但似乎什么都不适合我。甚至GitHub也没有有效的代码。我试着搜索UPnP、局域网、子网等,但什么也没找到

InetAddress localhost = InetAddress.getLocalHost();
byte[] ip = localhost.getAddress();
for (int i = 1; i <= 254; i++) {
    ip[3] = (byte)i;
    InetAddress address = InetAddress.getByAddress(ip);
    if (address.isReachable(1000)) {
        System.out.println(address + address.getHostAddress() + address.getAddress() + address.getHostName() + address.getCanonicalHostName());
    }
}
InetAddress localhost=InetAddress.getLocalHost();
字节[]ip=localhost.getAddress();

对于(int i=1;i而言,主要问题是您获取了错误的IP地址。InetAddress.getLocalHost()返回127.0.0.1,这只是您的设备

请改用中的Wifi IP地址:

ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

WifiInfo connectionInfo = wm.getConnectionInfo();
int ipAddress = connectionInfo.getIpAddress();
String ipString = Formatter.formatIpAddress(ipAddress);
下面是一个快速而肮脏的异步任务:

static class NetworkSniffTask extends AsyncTask<Void, Void, Void> {

  private static final String TAG = Constants.TAG + "nstask";

  private WeakReference<Context> mContextRef;

  public NetworkSniffTask(Context context) {
    mContextRef = new WeakReference<Context>(context);
  }

  @Override
  protected Void doInBackground(Void... voids) {
    Log.d(TAG, "Let's sniff the network");

    try {
      Context context = mContextRef.get();

      if (context != null) {

        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        WifiInfo connectionInfo = wm.getConnectionInfo();
        int ipAddress = connectionInfo.getIpAddress();
        String ipString = Formatter.formatIpAddress(ipAddress);


        Log.d(TAG, "activeNetwork: " + String.valueOf(activeNetwork));
        Log.d(TAG, "ipString: " + String.valueOf(ipString));

        String prefix = ipString.substring(0, ipString.lastIndexOf(".") + 1);
        Log.d(TAG, "prefix: " + prefix);

        for (int i = 0; i < 255; i++) {
          String testIp = prefix + String.valueOf(i);

          InetAddress address = InetAddress.getByName(testIp);
          boolean reachable = address.isReachable(1000);
          String hostName = address.getCanonicalHostName();

          if (reachable)
            Log.i(TAG, "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!");
        }
      }
    } catch (Throwable t) {
      Log.e(TAG, "Well that's not good.", t);
    }

  return null;
}

它没有按我所希望的方式工作:(.首先它尝试0.0.0.1、0.0.0.2等,我希望它检查192.168.0.1、192.168.0.2等。其次我只得到以下信息:
尝试ip:192.168.0.193
。我希望它这样说,例如:找到了名为“MY-PC”的ip 192.168.0.130。我该怎么做?0.0.0.0?确保你在真实设备上使用WiFi。编辑的代码只打印出带有关联主机名的可访问IP。jup你说得对,我在模拟器上测试了它(愚蠢的我),这就是它不起作用的原因。现在的问题是我没有得到任何名字。为什么不呢?这就是我得到的。例如192.168.0.105是sonos,但我看不到它的名字,其他应用程序也有。
Host:192.168.0.105(192.168.0.105)是可访问的!尝试ip:192.168.0.106尝试ip:192.168.0.107主机:192.168.0.107(192.168.0.107)是可访问的!尝试ip:192.168.0.108主机:192.168.0.108(192.168.0.108)是可访问的!尝试ip:192.168.0.109主机:192.168.0.109(192.168.0.109)是可访问的!
Odd。当我运行它时,我可以在可用时看到主机名。我的路由器列为:
主机:router.asus.com(192.168.1.1)是可以访问的!
我还看到很多其他设备,比如我的手机和带有主机名的打印机。顺便说一句,我添加了使用的权限。其他应用可能会尝试更复杂的方法。如果你找到一个可以做你想做的事情的应用,你可以随时联系开发人员或对其进行反编译,以查看事情是如何完成的。你在哪里找到的你是从哪里得到这些图标的?顺便说一句,对另一个问题发表评论并不会把它撞到前面page@lelloman这些都是来自应用程序,我想他们只是根据it@cricket_007我的目标不是…:P。我希望最初的提问者已经解决了这个问题,并想与大家分享
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
String macAdress = "5caafd1b0019";
String dataUrl = "http://api.macvendors.com/" + macAdress;
HttpURLConnection connection = null;
try {
    URL url = new URL(dataUrl);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.flush();
    wr.close();
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    StringBuffer response = new StringBuffer();
    String line;
    while ((line = rd.readLine()) != null) {response.append(line);response.append('\r');}
    rd.close();
    String responseStr = response.toString();
    Log.d("Server response", responseStr);
} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}}