2011-05-17 57 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

相關問題