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

纵然是瞬间 提交于 2019-11-30 16:56:13

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.

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