[英]ANDROID get all ip addresses on network
I would like to find all ip addresses on my network. 我想找到我网络上的所有IP地址。
For example i have activity with a button and a listview. 例如我有一个按钮和一个列表视图的活动。
When i click the button, i get all ip addresses on my netowrk and put into a listview. 当我单击按钮时,我会在网络上获得所有IP地址并放入列表视图。
Sorry for my english =) 对不起,我的英语=)
try this : 尝试这个 :
private static class NetworkSniffTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = "NetworkSniffTask";
private WeakReference<Context> mContextRef;
private NetworkSniffTask(Context context) {
mContextRef = new WeakReference<>(context);
}
@Override
protected Void doInBackground(Void... voids) {
Log.e(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.e(TAG, "activeNetwork: " + String.valueOf(activeNetwork));
Log.e(TAG, "ipString: " + String.valueOf(ipString));
String prefix = ipString.substring(0, ipString.lastIndexOf(".") + 1);
Log.e(TAG, "prefix: " + prefix);
for (int i = 0; i < 255; i++) {
String testIp = prefix + String.valueOf(i);
InetAddress name = InetAddress.getByName(testIp);
String hostName = name.getCanonicalHostName();
if (name.isReachable(1000))
Log.e(TAG, "Host:" + hostName);
}
}
} catch (Throwable t) {
Log.e(TAG, "Well that's not good.", t);
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.