0

I am trying to make an app that requires client's IP address. So far I have a code that generates the IP address of a local device but doesn't supply the IP address of the device that is connected to my local (own) device. The code below gives the IP address of the local device. How can I modify this code to obtain the client's IP address.

public  String getLocalIpAddress() {
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(android.content.Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        try {
            return InetAddress.getByName(String.format("%d.%d.%d.%d",
                    (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
                    (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff))).toString();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

Thanks.

Setnov
  • 1
  • Can you be more specific? It's unclear exactly what you mean by "client". Do you have two devices connected using WiFi Direct? – Daniel Nugent Oct 06 '17 at 00:33
  • Hello Daniel, thank you for getting back to me. Yes, I have two devices connected using WiFiP2P and what I meant earlier was "peer", that is the device that I am connecting to. – Setnov Oct 06 '17 at 00:38
  • Take a look at this answer: https://stackoverflow.com/a/5391763/4409409 – Daniel Nugent Oct 06 '17 at 00:56

0 Answers0