wifi search for the devices connected to the same network i.e other than acces point(for android)

旧时模样 提交于 2020-01-10 20:12:19

问题


i want to make a modification to my project and right now the project status is..... it is searches the available WiFi networks and shows the list with info of the network this works properly.Now i want to search and see the details of the devices connected to the network. Is there any way to find these devices ? Your comment will be useful for me, Thanks.


回答1:


Would you like to discover a specific device ? Or you need the list of all connected devices? The second I don't think is possible.

EDIT

Discovering specific devices:
Using UDP Broadcast. Some reference can be found here!

There are some protocols that are supported by some devices( routers, HDD, etc...), like UPNP!

If you develop a software on the device which you would like to discover you could create a UDP server listening on a specific port. Your client will just send a broadcast message on that port and your Server will send a response with the information you need. Here it is a simple example.




回答2:


You can loop over the IP ranges, and "ping" them. It is not the best / fastest method (UDP is better) but, it works in many cases. The sample code below returns the list of the IP addresses connected to the current network.

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;
}


来源:https://stackoverflow.com/questions/8445928/wifi-search-for-the-devices-connected-to-the-same-network-i-e-other-than-acces-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!