Android 使用ping列出本地网络上的设备

Android 使用ping列出本地网络上的设备,android,networking,local,ping,Android,Networking,Local,Ping,我试图创建一个函数,列出本地网络上所有连接的设备。 我要做的是ping addresspace x.x.x.0到x.x.x.255之间的任何地址,但它似乎无法正常工作。有人能解释或扩展我的代码吗?我确实收到了来自手机(10.0.0.17)和默认网关(10.0.0.138)的响应。后者甚至不应该存在(事实上,我不知道默认网关是什么,但忽略它)。但是我丢失了这台计算机的IP public ArrayList<InetAddress> getConnectedDevices(String

我试图创建一个函数,列出本地网络上所有连接的设备。 我要做的是ping addresspace x.x.x.0到x.x.x.255之间的任何地址,但它似乎无法正常工作。有人能解释或扩展我的代码吗?我确实收到了来自手机(10.0.0.17)和默认网关(10.0.0.138)的响应。后者甚至不应该存在(事实上,我不知道默认网关是什么,但忽略它)。但是我丢失了这台计算机的IP

public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
    ArrayList<InetAddress> ret = new ArrayList<InetAddress>();

    LoopCurrentIP = 0;

    //        String IPAddress = "";
    String[] myIPArray = YourPhoneIPAddress.split("\\.");
    InetAddress currentPingAddr;

    for (int i = 0; i <= 255; i++) {
        try {

            // build the next IP address
            currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
                    myIPArray[1] + "." +
                    myIPArray[2] + "." +
                    Integer.toString(LoopCurrentIP));

            // 50ms Timeout for the "ping"
            if (currentPingAddr.isReachable(50)) {
                if(currentPingAddr.getHostAddress() != YourPhoneIPAddress){
                    ret.add(currentPingAddr);

                }
            }
        } catch (UnknownHostException ex) {
        } catch (IOException ex) {
        }

        LoopCurrentIP++;
    }
    return ret;
}
public ArrayList getConnectedDevices(字符串YourPhoneIPAddress){
ArrayList ret=新的ArrayList();
LoopCurrentIP=0;
//字符串IPAddress=“”;
字符串[]myIPArray=YourPhoneIPAddress.split(“\\”);
InetAddress currentPingAddr;

对于(inti=0;i这里有一个稍微修改过的循环,它应该可以做到这一点(或者至少对我有效)

试试看{
网络接口iFace=网络接口
.getByInetAddress(InetAddress.getByName(YourIPAddress));

对于(int i=0;顺便说一句,我没有使用模拟器,我使用我的手机!这似乎是一个更好的解决方案,但现在我只在手机上获得本地IP,而不是笔记本电脑或服务器。我看到了这个功能块主线程,如何解决它?提前感谢
try {
    NetworkInterface iFace = NetworkInterface
            .getByInetAddress(InetAddress.getByName(YourIPAddress));

    for (int i = 0; i <= 255; i++) {

        // build the next IP address
        String addr = YourIPAddress;
        addr = addr.substring(0, addr.lastIndexOf('.') + 1) + i;
        InetAddress pingAddr = InetAddress.getByName(addr);

        // 50ms Timeout for the "ping"
        if (pingAddr.isReachable(iFace, 200, 50)) {
            Log.d("PING", pingAddr.getHostAddress());
        }
    }
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}