可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Starting from ICS, Wi-Fi Direct is introduced. Normally, we use the WifiP2pManager
class to operate on Wi-Fi Direct, but it seems that it can only retrieve the GroupOwner IP address after connected. But, actually, any device all came negotiate to become the GroupOwner. In Upper Application, we need to get the peer's IP address, or each peer's IP address in a group so that we can send/communicate with them.
How to get each IP address in Wi-Fi Direct? Include own IP address and each peer in the group?
回答1:
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 en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration 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 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...
回答2:
The best answer you can get is possibly the one from Mano:
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 is how I implemented it. When I connect a client to the group owner via WiFi Direct, I get the group owner's ip address, and send a message to the group owner over a socket. Something like:
Socket socket = new Socket(); socket.setReuseAddress(true); socket.connect((new InetSocketAddress(mIP, mPort)), SOCKET_TIMEOUT); OutputStream os = socket.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(new String("BROFIST"); oos.close(); os.close(); socket.close();
You already know mIP (the group owner's IP address), and only have to decide a mPort and receive the connection on the group owner like this:
Socket serverSocket = new ServerSocket(mPort); serverSocket.setReuseAddress(true); Socket client = serverSocket.accept(); ObjectInputStream objectInputStream = new ObjectInputStream(client.getInputStream()); Object object = objectInputStream.readObject(); if (object.getClass().equals(String.class) && ((String) object).equals("BROFIST")) { Log.d(TAG, "Client IP address: "+client.getInetAddress()); }
This is the actual code I'm using. I'm going to replace this message with some useful info, like a message object containing the MAC of the sender, which can be used to know about MAC-IP relations, since WifiP2pDevice only provides MAC and InetAddress the IP (Does anyone know if there's a way to get the MAC from an InetAddress object?)
回答3: