3

I am creating an Android app that sends data to a Firebase server. What I am unable to do is get the public IP address that is being used to connect to the Firebase server. When the user is on 3G, the code below gives the private IP which is useless since it changes every time the user connects to 3G on his mobile data. Hence, I want to get the public IP address that is being used to make a request to the Firebase server

If I am not wrong, the code below gives me the private IP address.

WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
int ipAddress = wifiInf.getIpAddress();
user782400
  • 1,617
  • 7
  • 30
  • 51

1 Answers1

2

If the device is connected to cellular data from the network service provider then you might not get the behavior that you are looking for. Their internal routers may or may not be sharing that IP, or blocking certain network traffic.

However, you can hit an external site that can provide this information.

Try one of the following:

  1. WhatIsMyIP.com offers a developer API for this sort of thing.

    https://www.whatismyip.com/api-documentation/

    Easier than just hitting the server itself.

  2. http://ifcfg.me/ip

    It just returns the IP address in it's most raw form.

Hopefully one of these will get you on the right path.

Ryan
  • 1,988
  • 4
  • 21
  • 34