Search code examples
androidandroid-wifi

Get IP Address of connected Wi fi in Android application


When I get the IP Address via the command wifiManager.getIpAddress() the app shuts down. Can someone tell me the reason why ? I am obtaining the desired ip address when I use the method at the bottom, but in the code for the same, when I try displaying the Ip address directly, it gives me an error

EDIT-

package com.example.socketserverclient;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

    final Context context = this;
    WifiManager wifi;
    TextView ipAddress, BSSID;
    Button getIP;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

//      String IpAddress = getLocalIpAddress();
        ipAddress = (TextView) findViewById(R.id.textIpAddress);
        BSSID = (TextView) findViewById(R.id.textBSSID);
        getIP = (Button) findViewById (R.id.button);
//      ipAddress.setText(IpAddress);
        getIP.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                String IpAddress = getLocalIpAddress();
//              ipAddress.setText(IpAddress);
            }

        });
    }
    private String getLocalIpAddress() {

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

        WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
        //gets the Ip address in hex form, We need to convert it to integer and then finally to string to display it 

        int myIp = myWifiInfo.getIpAddress(); 
        BSSID.setText(myIp);
        int intMyIp3 = myIp/0x1000000;
        int intMyIp3mod = myIp%0x1000000;

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

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

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


    return null;
        }

}

LOG CAT:

07-01 14:22:44.918: E/AndroidRuntime(21063): FATAL EXCEPTION: main
07-01 14:22:44.918: E/AndroidRuntime(21063): Process: com.example.socketserverclient, PID: 21063
07-01 14:22:44.918: E/AndroidRuntime(21063): android.content.res.Resources$NotFoundException: String resource ID #0xd01a8c0
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.content.res.Resources.getText(Resources.java:244)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.widget.TextView.setText(TextView.java:3888)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at com.example.socketserverclient.MainActivity.getLocalIpAddress(MainActivity.java:56)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at com.example.socketserverclient.MainActivity.access$0(MainActivity.java:48)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at com.example.socketserverclient.MainActivity$1.onClick(MainActivity.java:42)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.view.View.performClick(View.java:4438)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.view.View$PerformClick.run(View.java:18422)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.os.Handler.handleCallback(Handler.java:733)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.os.Looper.loop(Looper.java:136)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at android.app.ActivityThread.main(ActivityThread.java:5017)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at java.lang.reflect.Method.invoke(Method.java:515)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-01 14:22:44.918: E/AndroidRuntime(21063):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • The problem is in this line

    BSSID.setText(myIp);
    

    The problem is you are trying to pass an int in the setText method of your TextView.

    In short you are calling method setText(int resid) instead of setText(CharSequence text)

    So Android will try to look for the resource which you passed in the setText i.e. myIp which does not exist and you will get a ResourcesNotFoundException

    Change the code from

    BSSID.setText(myIp);
    

    to

    BSSID.setText(String.valueOf(myIp));