How to get the IP of the Android device to be used in Socket () in java?

0

I'm trying to implement peer-to-peer communication (device to device) and I need to get the real IP of the server device to then insert into the client but I can not. I'm using:

ServerSocket server = new ServerSocket(8000);
Log.v("TAG", server.getInetAddress().getHostAddress().toString());

and returns me only "::".

    
asked by anonymous 27.06.2017 / 03:49

1 answer

0

You will need to make a request to an external server to know the IP. Something like this:

https://github.com/kost/external-ip

================

In the case of Wi-Fi IP:

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

Turning on the manifest:

ACCESS_WIFI_STATE
    
27.06.2017 / 04:03