2016-09-05 8 views
6

मैं जावा के साथ अपने नेटवर्क पर सभी कनेक्टेड डिवाइस देखना चाहता हूं, लेकिन मैं इसे काम नहीं कर सकता। मैंने नीचे कुछ स्क्रीनशॉट संलग्न किए हैं, मैं इसे आउटपुट कैसे करना चाहता हूं। मैं नाम रखना चाहता हूं (उदाहरण के लिए "टीपी लिंक राउटर" या "नेक्सस 5 एक्स") और आईपी पता।Android पर स्थानीय नेटवर्क में सभी डिवाइसों के आईपी पते और नाम कैसे प्राप्त करें

मैंने Google और stackoverflow पर बहुत कुछ खोजा है, लेकिन मेरे लिए कुछ भी काम नहीं कर रहा था। यहां तक ​​कि गिटहब का कोई प्रभावी कोड नहीं है। मैंने यूपीएनपी, लोकल एरिया नेटवर्क, सबनेट्स इत्यादि की खोज करने की कोशिश की, लेकिन कुछ भी नहीं मिला।

InetAddress localhost = InetAddress.getLocalHost(); 
byte[] ip = localhost.getAddress(); 
for (int i = 1; i <= 254; i++) { 
    ip[3] = (byte)i; 
    InetAddress address = InetAddress.getByAddress(ip); 
    if (address.isReachable(1000)) { 
     System.out.println(address + address.getHostAddress() + address.getAddress() + address.getHostName() + address.getCanonicalHostName()); 
    } 
} 

Example 1 Example 2

मैं वास्तव में एक नकली (एक तरह से) प्रश्न मिला, लेकिन यह एक वर्ष से अधिक का जवाब दिया गया नहीं किया है। Source

+0

आप उन पहचानकर्ताओं को कहां से प्राप्त किया? – lelloman

+0

वैसे, किसी अन्य प्रश्न पर टिप्पणी करने से इसे सामने वाले पृष्ठ –

+0

@lelloman में नहीं लगाया जाता है, जो मुझे लगता है कि वे इसे – Jason

उत्तर

6

मुख्य समस्या यह है कि आप गलत आईपी पता पकड़ रहे हैं। InetAddress.getLocalHost() 127.0.0.1 लौटा रहा है और यह सिर्फ आपका डिवाइस है।

बजाय में वाईफ़ाई आईपी पते का उपयोग करें:

ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 

WifiInfo connectionInfo = wm.getConnectionInfo(); 
int ipAddress = connectionInfo.getIpAddress(); 
String ipString = Formatter.formatIpAddress(ipAddress); 

यहाँ एक त्वरित और गंदी AsyncTask कि करता है कि:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
नहीं

:

static class NetworkSniffTask extends AsyncTask<Void, Void, Void> { 

    private static final String TAG = Constants.TAG + "nstask"; 

    private WeakReference<Context> mContextRef; 

    public NetworkSniffTask(Context context) { 
    mContextRef = new WeakReference<Context>(context); 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
    Log.d(TAG, "Let's sniff the network"); 

    try { 
     Context context = mContextRef.get(); 

     if (context != null) { 

     ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
     WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 

     WifiInfo connectionInfo = wm.getConnectionInfo(); 
     int ipAddress = connectionInfo.getIpAddress(); 
     String ipString = Formatter.formatIpAddress(ipAddress); 


     Log.d(TAG, "activeNetwork: " + String.valueOf(activeNetwork)); 
     Log.d(TAG, "ipString: " + String.valueOf(ipString)); 

     String prefix = ipString.substring(0, ipString.lastIndexOf(".") + 1); 
     Log.d(TAG, "prefix: " + prefix); 

     for (int i = 0; i < 255; i++) { 
      String testIp = prefix + String.valueOf(i); 

      InetAddress address = InetAddress.getByName(testIp); 
      boolean reachable = address.isReachable(1000); 
      String hostName = address.getCanonicalHostName(); 

      if (reachable) 
      Log.i(TAG, "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!"); 
     } 
     } 
    } catch (Throwable t) { 
     Log.e(TAG, "Well that's not good.", t); 
    } 

    return null; 
} 

यहाँ अनुमतियाँ हैं सभी राउटर इसे अनुमति देते हैं, इसलिए नाम किसी अन्य तरीके से प्राप्त करने के लिए मैक एड्रेस को एपीआई में भेजना और ब्रांड नाम बीएसी प्राप्त करना है बदले में के।

String macAdress = "5caafd1b0019"; 
String dataUrl = "http://api.macvendors.com/" + macAdress; 
HttpURLConnection connection = null; 
try { 
    URL url = new URL(dataUrl); 
    connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
    wr.flush(); 
    wr.close(); 
    InputStream is = connection.getInputStream(); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    StringBuffer response = new StringBuffer(); 
    String line; 
    while ((line = rd.readLine()) != null) {response.append(line);response.append('\r');} 
    rd.close(); 
    String responseStr = response.toString(); 
    Log.d("Server response", responseStr); 
} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}} 
+0

साझा करना चाहता हूं, यह जिस तरह से मैं चाहता हूं वह काम नहीं करता है :(। सबसे पहले यह 0.0.0.1, 0.0.0.2 इत्यादि का प्रयास करता है और मैं इसे चाहता हूं 1 9 2.168.0.1, 1 9 2.168.0.2, आदि की जांच करें। सबसे पहले मैं केवल यह प्राप्त कर रहा हूं: 'आईपी आज़मा रहा है: 1 9 2.168.0.1 9 3'। मैं इसे उदाहरण के लिए कहना चाहता हूं: आईपी 1 9 2.168.0.130 नाम "आईई-पीसी" मैं ऐसा कैसे कर सकता हूं? – Jason

+0

0.0.0.0? सुनिश्चित करें कि आप वास्तविक डिवाइस पर वाईफाई पर हैं। और संपादित कोड केवल संबंधित होस्ट नाम के साथ पहुंच योग्य आईपी प्रिंट करता है। – DataDino

+0

jup आप सही हैं, मैंने इसका परीक्षण किया एम्यूलेटर (बेवकूफ मुझे), यही कारण है कि यह काम नहीं किया। समस्या अब यह है कि मुझे कोई नाम नहीं मिल रहा है। क्यों नहीं? यह वही है जो मुझे मिल रहा है। उदाहरण के लिए 1 9 2.168.0.105 सोनोस है, लेकिन मैं नहीं करता इसका नाम देखें, और अन्य ऐप्स करते हैं। 'होस्ट: 1 9 2.168.0.105 (1 9 2.168.0.105) पहुंच योग्य है! आईपी आज़माकर: 192.168.0.106 आईपी आज़मा रहा है: 1 9 2.168.0.107 होस्ट: 1 9 2.168.0.107 (1 9 2.168.0.107) पहुंच योग्य है! आईपी आज़मा रहा है: 1 9 2.168.0.108 होस्ट: 1 9 2.168.0.108 (1 9 2.168.0.108) पहुंच योग्य है! आईपी आज़मा रहा है: 1 9 2.168.0.10 9 होस्ट: 1 9 2.168.0.10 9 (1 9 2.168.0.10 9) पहुंच योग्य है! ' – Jason

संबंधित मुद्दे

 संबंधित मुद्दे