获取在同一网络中连接的所有设备的列表

时间:2015-07-02 07:33:13

标签: android sockets network-programming android-networking

如何获取在同一WiFi网络中连接的所有设备的列表。我确实发现几个应用程序在Google Play上完全可用,例如"谁在我的WiFi上#34;和" WiFi Inspector"。我想在我的应用程序中实现相同的功能。

1 个答案:

答案 0 :(得分:0)

我找到这段代码来显示同一个WI-FI的所有连接的Ips:

private int LoopCurrentIP = 0;
String ad ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //ArrayList<InetAddress> ar = getConnectedDevices( );

}



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));
            ad = currentPingAddr.toString();   /////////////////
            Log.d("MyApp",ad);                 //////////////

            // 50ms Timeout for the "ping"
            if (currentPingAddr.isReachable(50)) {

                ret.add(currentPingAddr);
                ad = currentPingAddr.toString();        /////////////////
                Log.d("MyApp",ad);                     //////////////
            }
        } catch (UnknownHostException ex) {
        } catch (IOException ex) {
        }

        LoopCurrentIP++;
    }
    return ret;
}