如何获取Wi-Fi Direct中的每个IP地址?包括自己的IP地址和群组中的每个对等设备的IP地址?
我遇到了相同的问题。由于两个设备都知道组所有者的IP地址,所以已经可以向组所有者发送消息。您发送的第一条消息可以包含另一个设备的IP地址; 从那时起,双向通信就成为可能。
以下是在Java中检索您的IP地址的可能方式:
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());
将该IP地址包装在可序列化的对象中,像发送任何其他对象一样将其发送到组所有者。将其视为WiFi Direct协议的第一步...现在,组所有者也有一个IP可以用来发送回答。
这对我有效,虽然我认为自己必须亲自实现并且只能轻松找到组所有者IP(info.groupOwnerAddress.getHostAddress(); //其中info是WifiP2pInfo实例),这有点奇怪。也许还有一种类似的方法来检索其他对等方的IP,但我找不到。如果您找到了,请与我联系。
祝好运...
你可能能得到的最好答案可能来自Mano:
我遇到了同样的问题。由于两个设备都知道组所有者的IP地址,因此已经可以向组所有者发送消息。您发送的第一条消息可以包含另一个设备的IP地址;从那时起,双向通信就成为可能了。
这是我如何实现的。当我通过WiFi Direct将客户端连接到组所有者时,我会获取组所有者的IP地址,并通过套接字向组所有者发送消息。类似这样:
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();
你已经知道mIP(组所有者的IP地址),只需要决定一个mPort并在组所有者上接收连接,像这样: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());
}
这是我正在使用的实际代码。我将用一些有用的信息替换这条消息,比如一个包含发送方MAC地址的消息对象,可以用来了解MAC-IP关系,因为WifiP2pDevice只提供MAC地址而InetAddress提供IP地址(是否有办法从一个InetAddress对象中获取MAC地址?)。
public static String getIPFromMac(String MAC) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
// Basic sanity check
String device = splitted[5];
if (device.matches(".*p2p-p2p0.*")){
String mac = splitted[3];
if (mac.matches(MAC)) {
return splitted[0];
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
我有另一种方法!
当您在两个(或更多)设备之间创建连接时,无法设置谁是所有者,谁是客户端。
唯一的信息是当 BroadCast 接收器连接时,您会接收到一个“WifiP2pInfo”对象。此对象包含两个有趣的信息:
从那里,您拥有了所需的所有内容!
如果您是群主 => 监听连接
否则,请使用 IP 地址向所有者创建连接。
if (mWifiP2PInfo.isGroupOwner) {
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
} else {
// If the client is not listening when the server create the connection, the connection is not established => Try again
int retry = 10;
socket = new Socket();
socket.bind(null);
do {
socket.connect((new InetSocketAddress(mWifiP2PInfo.groupOwnerAddress, port)), 500);
retry--;
} while (!socket.isConnected() && retry > 0);
}
DatagramSocket socket=null;
try {
socket=new DatagramSocket();
socket.connect((new InetSocketAddress(host, port)));
String address=socket.getLocalAddress().getHostAddress();
ByteBuffer bb=ByteBuffer.allocate(2+address.length());
bb.putChar('I');
bb.put(address.getBytes());
DatagramPacket pkt=new DatagramPacket(bb.array(),2+address.length());
socket.send(pkt);
Log.d(WiFiDirectActivity.TAG,"address"+address+"dest"+host);
Log.d(WiFiDirectActivity.TAG,"send");
} catch (SocketException e) {
Log.e(WiFiDirectActivity.TAG,"error socketException");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (socket != null) {
if (socket.isConnected()) {
socket.close();
}
}
host
是从连接信息中获取的组所有者的地址。
public static String getIpAddress() {
try {
List<NetworkInterface> interfaces = Collections
.list(NetworkInterface.getNetworkInterfaces());
/*
* for (NetworkInterface networkInterface : interfaces) { Log.v(TAG,
* "interface name " + networkInterface.getName() + "mac = " +
* getMACAddress(networkInterface.getName())); }
*/
for (NetworkInterface intf : interfaces) {
if (!getMACAddress(intf.getName()).equalsIgnoreCase(
Globals.thisDeviceAddress)) {
// Log.v(TAG, "ignore the interface " + intf.getName());
// continue;
}
if (!intf.getName().contains("p2p"))
continue;
Log.v(TAG,
intf.getName() + " " + getMACAddress(intf.getName()));
List<InetAddress> addrs = Collections.list(intf
.getInetAddresses());
for (InetAddress addr : addrs) {
// Log.v(TAG, "inside");
if (!addr.isLoopbackAddress()) {
// Log.v(TAG, "isnt loopback");
String sAddr = addr.getHostAddress().toUpperCase();
Log.v(TAG, "ip=" + sAddr);
boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
if (isIPv4) {
if (sAddr.contains("192.168.49.")) {
Log.v(TAG, "ip = " + sAddr);
return sAddr;
}
}
}
}
}
} catch (Exception ex) {
Log.v(TAG, "error in parsing");
} // for now eat exceptions
Log.v(TAG, "returning empty ip address");
return "";
}
public static String getMACAddress(String interfaceName) {
try {
List<NetworkInterface> interfaces = Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (interfaceName != null) {
if (!intf.getName().equalsIgnoreCase(interfaceName))
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null)
return "";
StringBuilder buf = new StringBuilder();
for (int idx = 0; idx < mac.length; idx++)
buf.append(String.format("%02X:", mac[idx]));
if (buf.length() > 0)
buf.deleteCharAt(buf.length() - 1);
return buf.toString();
}
} catch (Exception ex) {
} // for now eat exceptions
return "";
/*
* try { // this is so Linux hack return
* loadFileAsString("/sys/class/net/" +interfaceName +
* "/address").toUpperCase().trim(); } catch (IOException ex) { return
* null; }
*/
}