android 截取字符串内的ip地址,如何在Android上获取本地网络中所有设备的IP地址和名称...

DataDino..

14

主要问题是你抓错了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 {

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

private WeakReference mContextRef;

public NetworkSniffTask(Context context) {

mContextRef = new WeakReference(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;

}

以下是权限:

并非所有路由器都允许这样做,因此以其他方式获取名称是将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();}}

其他应用可能会尝试更复杂的东西.如果您发现某个应用程序可以执行您想要执行的操作,则可以随时与开发人员联系或对其进行反编译以查看完成的操作. (2认同)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值