1

我想对我的项目进行修改,现在项目状态是.....它正在搜索可用的 WiFi 网络并显示列表,其中包含正常工作的网络信息。现在我想搜索并查看详细信息连接到网络的设备。有没有办法找到这些设备?你的评论对我有用,谢谢。

4

2 回答 2

12

您可以遍历 IP 范围,并“ping”它们。它不是最好/最快的方法(UDP 更好),但它在许多情况下都有效。下面的示例代码返回连接到当前网络的 IP 地址列表。

private int LoopCurrentIP = 0;

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)) {

                ret.add(currentPingAddr);
            }
        } catch (UnknownHostException ex) {
        } catch (IOException ex) {
        }

        LoopCurrentIP++;
    }
    return ret;
}
于 2012-01-06T18:36:31.963 回答
5

您想发现特定设备吗?或者您需要所有已连接设备的列表?第二个我觉得不可能。

编辑

发现特定设备:
使用UDP Broadcast. 一些参考可以在这里找到!

某些设备(路由器、硬盘等)支持某些协议,例如UPNP

如果您在想要发现的设备上开发软件,您可以创建一个UDP侦听特定端口的服务器。您的客户将只broadcast在该端口上发送一条消息,您Server将发送包含您需要的信息的响应。 是一个简单的例子。

于 2011-12-09T14:32:08.607 回答