如何获取当前wifi网络中的活动设备列表?

时间:2011-05-17 19:24:35

标签: android

很抱歉,如果这个问题已经存在,但我找不到任何答案,那可以帮助我。

我正试图找到一种方法来获取活动网络中存在的ip + mac列表。

有没有办法用java或c ++来实现它,然后将其推荐给Android的java应用程序?

2 个答案:

答案 0 :(得分:3)

wifi netNetwork中的这段代码搜索设备...

package com.example.deviceswichisconnected;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

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

答案 1 :(得分:0)

查看Bonjour / Zeroconfhttp://android.noisepages.com/2010/02/yes-android-can-do-zeroconfbonjour-jmdns/

但是,除非您以某种方式访问​​其路由器,否则不要希望生成LAN上所有设备的列表。