5

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

Mgamerz
  • 2,872
  • 2
  • 27
  • 48
  • 1
    I found that I can execute netcfg to do some of the work. It's really ugly but I guess it will work... Lots of regexing... This seems overly complicated to do such a simple thing. – Mgamerz Jan 27 '13 at 07:53
  • 2
    try reading this answer: http://stackoverflow.com/a/13007325/1056359 – thepoosh Jan 27 '13 at 08:11
  • I'm looking at that answer right now. Since I don't know what the interface name is though (it seems to vary) I could possibly try a REGEX. Unfortunately this doesn't work on ICS since the interface seems down to some methods and up to others and it's down on this. – Mgamerz Jan 27 '13 at 20:55
  • I haven't fully got mine to work yet but once I'll do I'll post it as an answer so people can find it. – Mgamerz Jan 27 '13 at 23:46
  • Still working on it. Keep having to do other parts of my app first so I can get to the part that matters. – Mgamerz Jan 31 '13 at 03:27

1 Answers1

1
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();

neeraj
  • 477
  • 1
  • 3
  • 10
  • That doesn't get you any IP address only the wifi address which I specifically state that I'm not looking for. – Mgamerz Feb 10 '13 at 16:35
  • 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(); – neeraj Feb 11 '13 at 22:01
  • The whole reason I need the IP address is so I can create a socket connection. If I don't know the address I can't attempt to connect. – Mgamerz Feb 11 '13 at 22:04
  • i am not able to get what ip address are you looking for... i have provided you the method to get both your and client ip address... – neeraj Feb 11 '13 at 22:22
  • Misread your post a few times, now I see what you mean... kind of. With the socket.getInetAddress(). Sorry about that. If you edit your answer I'll restore the upvote. – Mgamerz Feb 11 '13 at 22:27
  • Thanks. I'll still post my answer when I fix my netcfg regex expression. – Mgamerz Feb 12 '13 at 04:43
  • Can you send me code of Socket programming, where we need to place IP address. Above Code provide the IP address of connected network. Not WIFI Direct IP address. – Teja Jun 02 '14 at 11:19