[color=blue][b]Getting IP Address of the Android Device[/b][

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[color=blue][b]Getting IP Address of the Android Device[/b][

Postby vikrant » Mon Mar 22, 2010 6:28 am

There can be 3 ip's connected with the android device (only one at a time)
1. WiFi IP to which the device is connected
2. IP allocated by the mobile operator (3G Network) which is dynamically allocated to the device(Note:- it can be a static ip as well as some/most devices have this option for static ip)
3. Ethernet IP -- If your device can be run in a host mode and made to use ethernet for internet.

[b]Code Snippets:- [/b]

For mobile network or ethernet ip
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. java.net.Socket conn = null;
  2.  
  3.                                 try {
  4.  
  5.                                         conn = new java.net.Socket("www.google.com", 80);
  6.  
  7.                                 } catch (UnknownHostException e1) {
  8.  
  9.                                        
  10.  
  11.                                         e1.printStackTrace();
  12.  
  13.                                 } catch (IOException e1) {
  14.  
  15.                                        
  16.  
  17.                                         e1.printStackTrace();
  18.  
  19.                                 }
  20.  
  21.                 String ipAddress = conn.getLocalAddress().toString();
  22.  
  23.                 InetAddress ipAddress_site =conn.getInetAddress();
  24.  
  25.                 Toast.makeText(IPAddress.this," Device IP"+ipAddress +"IPv6 Site IP"+ipAddress_site,Toast.LENGTH_LONG).show();
  26.  
  27.                 try {
  28.  
  29.                                         conn.close();
  30.  
  31.                                 } catch (IOException e) {
  32.  
  33.                                         // TODO Auto-generated catch block
  34.  
  35.                                         e.printStackTrace();
  36.  
  37.                                 }
  38.  
  39.            
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



For WiFi


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public int WiFi_Info()
  2.  
  3.     {
  4.  
  5.     WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
  6.  
  7.         WifiInfo wf =wm.getConnectionInfo();
  8.  
  9.         link_speed=wf.getLinkSpeed();
  10.  
  11.         Rssi=wf.getRssi();
  12.  
  13.         String Mac_Address=wf.getMacAddress(); 
  14.  
  15.                int ip_add = wf.getIpAddress();
  16.  
  17.                return ip_add;
  18.  
  19.     }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



Note:- Above methods return IP as integers and to convert it into human(geeks :)) readable form xxx.xxx.xxx.xxx, we need to convert this here is the fn that can do this:-
Function to convert integer into ip address --
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static String intToIp(int i) {
  2.  
  3.                 return ( (i& 0xFF) + "." +((i >>8)& 0xFF)+"."+((i>>16)&0xFF)+"."+((i >> 24 )&0xFF));
  4.  
  5.        
  6.  
  7.     }
  8.  
  9.    
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Hope it is useful -- :roll:
Share n Learn
User avatar
vikrant
Freshman
Freshman
 
Posts: 6
Joined: Tue Oct 14, 2008 7:26 am
Location: CA,USA

Top

How to detect ethernet connection ?

Postby saamyandroid » Mon Apr 12, 2010 11:16 am

How can i detect ethernet connection in android.

ConnectivityManager helps only to detect following
Wi-Fi, GPRS, UMTS .


i want to detect ethernet connection.
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests