如何在Android上获取本地网络中所有设备的IP地址和名称 [英] How to get IP address and names of all devices in local network on Android
问题描述
我想用java看到我网络上的所有连接设备,但是我无法使用它。我在下面附上了一些截图,说明我希望如何输出。我想要这个名字(例如TP Link Router或Nexus 5X)和IP地址。
I want to see all the connected devices on my network with java, but I can't get it working. I have attached some screenshots below of how I want it to be output. I would like to have the name (for example "TP Link Router" or "Nexus 5X") and the IP address.
我在google和stackoverflow上搜索了很多,但似乎没有什么对我有用。即使是GitHub也没有有效的代码。我尝试搜索UPnP,局域网,子网等,但一无所获。
I have searched a lot on google and stackoverflow, but nothing seemed to work for me. Even GitHub has no effective code. I tried searching for UPnP, Local Area Network, subnets, etc, but found nothing.
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());
}
}
我确实发现了一个重复(某种)问题,但一年多来都没有回答。 来源
I did in fact find a duplicate (sort of) question, but it hasn't been answered for over a year. Source
推荐答案
主要问题是您正在抓取错误的IP地址。 InetAddress.getLocalHost()返回127.0.0.1,这只是你的设备。
The main problem is that you're grabbing the wrong IP address. InetAddress.getLocalHost() is returning 127.0.0.1 and that's just your device.
改为使用Wifi IP地址:
Use the Wifi IP address in instead:
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:
Here is a quick and dirty AsyncTask that does that:
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并获得品牌名称作为回报。
Not all routers allow this, so to get the names in a other way is to send the mac adress to an api and get the brand name back in return.
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();}}
这篇关于如何在Android上获取本地网络中所有设备的IP地址和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!