Search code examples
androidipandroid-wifiandroid-networking

Get my wifi ip address Android


How can I get the ip address of my phone when it is connected under wifi?

I found a method here but it returns something like 24.182.239.255 even if I'm under wifi and I expect something like 192.168.1.10.

I'd like something like:

if (you are under wifi)
    String ip4 = getWifiIP()
else
    String ip4 = getIPAddress with the method linked before

Many thanks!


Solution

  • If you would like to get the private IP address of your device when connected to Wi-Fi, you can try this.

    WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    String ipAddress = Formatter.formatIpAddress(ip);
    

    Be sure to add the permission

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

    to your manifest.