How to get the IP address of the device in WIFI DIRECT?

how to get each Device's Ip Address in WIFI Direct Scenario

  • start from ICS, the WIFI Direct is introduced, we normally use WIFIP2PManager Class to operate the WIFI Direct, But I found seems it only can retreive the GroupOwner IpAddress after Connected. But, actually, Any devcice all came negotiate to become the GroupOwner, in Upper Application, We need get peer's ip address, Or each peer's ip address in a group so that we can send/communicate with them. Then how to get each ip address in WIFIDirect?? include own ipaddress and each peer in the group?

  • Answer:

    The list of the detected devices can be retrieved using WifiP2pDeviceList. For this you need to implement the PeerListListener . This interface provides onPeerAvailable() asynchronous method the tells your activity when peers are available, in that method we get the WifiP2pDeviceList and when we call yourWifiP2pManagerObj.requestPeers(mychannel, myPeerListListener); we can retrive the list. I did it like this :d ArrayList<WifiP2pDevice> peerslist; peerListListener = new PeerListListener() { public void onPeersAvailable(WifiP2pDeviceList peers) { peerslist = (ArrayList<WifiP2pDevice>) peers.getDeviceList(); return; } }; After getting the list //obtain a peer from the WifiP2pDeviceList WifiP2pDevice device; WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; manager.connect(channel, config, new ActionListener() { @Override public void onSuccess() { //success logic } @Override public void onFailure(int reason) { //failure logic } }); and then transfer the data by creating sockets for transferring and receiving data. you can see the source of WifiDirect example application that comes with the SDK. and read this its very clear and helpful to get started http://developer.android.com/guide/topics/wireless/wifip2p.html. Hope this helps. Correct me if i am wrong and i am curious about how you are going to test your wifidirect application. According to my knowledge emulator doesn't support it yet and androidx86 too. correct me if there are any mistakes in the answer.

nicefuture at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I encountered the same problem. Since both devices know the group owner's ip, it is already possible to send a message to the group owner. The first message you send can contain the ip address of the other device; from then on, bidirectional communication is possible. Here's a possibility for retrieving your ip in java: private byte[] getLocalIPAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { if (inetAddress instanceof Inet4Address) { // fix for Galaxy Nexus. IPv4 is easy to use :-) return inetAddress.getAddress(); } //return inetAddress.getHostAddress().toString(); // Galaxy Nexus returns IPv6 } } } } catch (SocketException ex) { //Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex); } catch (NullPointerException ex) { //Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex); } return null; } private String getDottedDecimalIP(byte[] ipAddr) { //convert to dotted decimal notation: String ipAddrStr = ""; for (int i=0; i<ipAddr.length; i++) { if (i > 0) { ipAddrStr += "."; } ipAddrStr += ipAddr[i]&0xFF; } return ipAddrStr; } ip = getDottedDecimalIP(getLocalIPAddress()); Wrap this ip in a Serializable object and send it to the group owner like you would send any other object. Consider this the first step in your wifi-direct protocol... Now, the group owner also has an IP to send answers to. This works for me, although I think it's weird that I had to implement this myself and I could only find the group owner ip easily (info.groupOwnerAddress.getHostAddress(); //with info a WifiP2pInfo instance). Maybe there is a comparable way to retrieve the ip of the other peers, but I couldn't find it. Please contact me if you do. Good luck...

Mano

To Pruthvi Nag (I can't reply to him directly), about your question : "I am curious about how you are going to test your wifidirect application ?" You can test this on two Galaxy Nexus, this phone is WiFiDirect enabled and can run on Android 4.0 (API level 14)

Fabien D.

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.