Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Android network interface IP address

I am writing a wifi-direct application for Android. I am trying to make a socket connection to a device. Since the group owner intent function doesn't seem to work (it just assigns randomly it appears) I have to find a way to transfer the clients IP address to the host. The only address I know of is the host device which comes in the group info object that WifiP2pManager can get me. I know which device is the host and which one is the client so I can open a socket for a connection to arrive or attempt to connect to the other one.

What I need to do is find a way to transfer the device's IP address of the Wifi P2P (Wifi Direct) client if the 'host' device is the group owner. If the host is the group owner, I have no way to connect to the socket on the client. It's a bit confusing but that's how it works.

I've seen things like get the IP address from the ARP table, but the ARP table seems to clear itself after only a few seconds (like a minute) and on ICS since the wifi interface is disabled for Wifi direct I don't even see anything in the arp table.

I feel like this should be easy but I'm not a big linux user so I don't know what file would hold the network interface configurations. Is there a way to get hte IP addresses of network interfaces? Or at least the Wifi P2P interface? (Note: This is not the wifi address. It's similar to the tethering address except it's Wifi Direct. WifiManager does not return this)

Thanks,
Mgamerz

like image 416
Mgamerz Avatar asked Nov 13 '22 12:11

Mgamerz


1 Answers

DhcpInfo dhcpInfo = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo();
String ipaddress = intToIp(dhcpInfo.ipAddress)

intToIp(int integer) {
        return (integer & 0xFF) + "." + ((integer >> 8) & 0xFF) + "."
                + ((integer >> 16) & 0xFF) + "." + ((integer >> 24) & 0xFF);
    }

Above code should help you get the ipaddress...

to get the ip address of the client who is connecting to host through a socket you may use.. clientSocket = this.serverSocket.accept(); clientSocket.getInetAddress();

like image 127
neeraj Avatar answered Nov 15 '22 06:11

neeraj