开发者

How to get the list of active devices in current wifi network?

Sorry if such question already exi开发者_Python百科st, but I can't find any answer, that can help me.

I am trying to find a way to get a list of ip+mac of deivces exist in active network.

Is there a way to do it with java or with c++ and then impliment it to java app for android ?


this piece of code search devices in 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;
    }
}


Have a look at Bonjour/Zeroconf: http://android.noisepages.com/2010/02/yes-android-can-do-zeroconfbonjour-jmdns/

But don't get your hopes up for generating a list of all the devices on your LAN, unless you somehow have access to its router.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜