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
Using java Syntax Highlighting
- java.net.Socket conn = null;
- try {
- conn = new java.net.Socket("www.google.com", 80);
- } catch (UnknownHostException e1) {
- e1.printStackTrace();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- String ipAddress = conn.getLocalAddress().toString();
- InetAddress ipAddress_site =conn.getInetAddress();
- Toast.makeText(IPAddress.this," Device IP"+ipAddress +"IPv6 Site IP"+ipAddress_site,Toast.LENGTH_LONG).show();
- try {
- conn.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
For WiFi
Using java Syntax Highlighting
- public int WiFi_Info()
- {
- WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
- WifiInfo wf =wm.getConnectionInfo();
- link_speed=wf.getLinkSpeed();
- Rssi=wf.getRssi();
- String Mac_Address=wf.getMacAddress();
- int ip_add = wf.getIpAddress();
- return ip_add;
- }
Parsed in 0.031 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 --
Using java Syntax Highlighting
- public static String intToIp(int i) {
- return ( (i& 0xFF) + "." +((i >>8)& 0xFF)+"."+((i>>16)&0xFF)+"."+((i >> 24 )&0xFF));
- }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Hope it is useful --


