我想用java看到我网络上的所有连接设备,但是我无法让它工作.我在下面附上了一些截图,说明我希望如何输出.我想要名称(例如“TP link Router”或“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()); }}
事实上,我确实找到了一个重复(某种)问题,但一年多来都没有回答. Source
解决方法:
主要问题是你抓错了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);
这是一个快速而肮脏的AsyncTask,它可以做到这一点:
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;}
以下是权限:
<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" />
并非所有路由器都允许这样做,因此以另一种方式获取名称是将mac地址发送到API并获得品牌名称作为回报.
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();}}
总结 以上是内存溢出为你收集整理的java – 如何获取Android上本地网络中所有设备的IP地址和名称全部内容,希望文章能够帮你解决java – 如何获取Android上本地网络中所有设备的IP地址和名称所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)