Sunday, January 16, 2011

Get Wifi IP of Android device, using WifiManager

It's a simple exercise to retrieve my Wifi Ip address using WifiManager.

Get Wifi IP of Android device, using WifiManager

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My WifiManager:"
/>
<TextView
android:id="@+id/WifiManager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My WifiInfo"
/>
<TextView
android:id="@+id/WifiInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Wifi IP:"
/>
<TextView
android:id="@+id/Ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


AndroidWifiIp.java
package com.exercise.AndroidWifiIp;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidWifiIp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textWifiManager = (TextView)findViewById(R.id.WifiManager);
TextView textWifiInfo = (TextView)findViewById(R.id.WifiInfo);
TextView textIp = (TextView)findViewById(R.id.Ip);

WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int myIp = myWifiInfo.getIpAddress();

textWifiManager.setText(myWifiManager.toString());
textWifiInfo.setText(myWifiInfo.toString());

int intMyIp3 = myIp/0x1000000;
int intMyIp3mod = myIp%0x1000000;

int intMyIp2 = intMyIp3mod/0x10000;
int intMyIp2mod = intMyIp3mod%0x10000;

int intMyIp1 = intMyIp2mod/0x100;
int intMyIp0 = intMyIp2mod%0x100;

textIp.setText(String.valueOf(intMyIp0)
+ "." + String.valueOf(intMyIp1)
+ "." + String.valueOf(intMyIp2)
+ "." + String.valueOf(intMyIp3)
);
}
}


Also have to modify AndroidManifest.xml to grant permission of android.permission.ACCESS_WIFI_STATE.
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />


Download the files.

Related article: Monitor Wifi status and information with BroadcastReceiver



10 comments:

  1. i have a print button onclicking the button i want the printer to print my xml file????
    I have no idea how to achieve this....
    And need little light and help, thnk u

    ReplyDelete
  2. hello edward,

    Sorry! I have no idea how to print in Android.

    ReplyDelete
  3. Is it possible to get the RSSI of individual networks in the ScanResult list? These are not the pre-configured networks.
    Please note that i need the RSSI value and not the signal strength in dBm. Thanks

    ReplyDelete
  4. Thanks for ur reply.

    But i'd wanted the following:

    1. scan the available networks- which is generally done via startScan(); The results are obtained on a list.

    2. get the "RSSI" value for each found-
    which is my question.

    A little hint/idea would do great. Thanks.

    ReplyDelete
  5. how to establish the wifi connection for running this app on android...

    ReplyDelete
  6. Thanks buddy nice work

    ReplyDelete
  7. hello sir, thanks for the tutorial..
    can i ask something... what is the different on this three line?

    int intMyIp3 = myIp/0x1000000;
    int intMyIp3mod = myIp%0x1000000;

    int intMyIp2 =intMyIp3mod/0x10000;
    int intMyIp2mod = intMyIp3mod%0x10000;

    int intMyIp1 = intMyIp2mod/0x100;
    int intMyIp0 = intMyIp2mod%0x100;

    ReplyDelete
  8. Hello Fai Zal,

    It's used to get the four group of digits, ex. 192.168.1.100

    ReplyDelete
  9. Good work. Can you pls help me to display all the wifi services with its IP address??

    ReplyDelete