2011-05-17 74 views
1

对不起,如果这样的问题已经存在,但我找不到任何答案,这可以帮助我。如何获取当前wifi网络中活动设备的列表?

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

有没有办法用java或C++来实现它,然后将它提供给java的android应用程序?

回答

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

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

但是,不要让您的希望为您的局域网上生成所有设备的列表,除非你以某种方式访问​​它的路由器。

+0

嗯..看起来它可以帮助,但我不能包括此的javax lib添加到项目中。它会抛出错误“导入javax.jmdns无法解析”我认为这是因为android并未实现所有Java SE引擎:( – user758030 2011-05-18 13:36:38

相关问题