Arnout
Arnout

Reputation: 157

How to find local IP adresses

I want to make an app which detects all ip4 addresses in the network. I searched a bit on the internet and found a YouTube tutorial for a JAVA desktop application. Becouse android apps can be programmed in JAVA i used that same code for the app. code:

try {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while(e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration ee = n.getInetAddresses();

        while(ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();
            System.out.println(i.getHostAddress());
        }
    }
}
catch (Exception ex) {
}

for a desktop JAVA application this code will be fine. However when i try and run this code with the android sdk also based on JAVA i get null. Can any one explain this to me? code in the android part:

try{
   Enumeration e = NetworkInterface.getNetworkInterfaces();
   while(e.hasMoreElements()){
       NetworkInterface n = (NetworkInterface) e.nextElement();
       Enumeration ee = n.getInetAddresses();
       int ii = 0;
       while(ee.hasMoreElements()){
           InetAddress i = (InetAddress) ee.nextElement();
           ips[ii] = i.getHostAddress();
           ii++;
        }
   }

} catch (Exception ex) {

 }

Upvotes: 0

Views: 144

Answers (1)

Melih Altıntaş
Melih Altıntaş

Reputation: 2535

// AndroidManifest.xml permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Upvotes: 1

Related Questions