如何获得了Wi-Fi直连方案中的每个设备的IP地址? [英] How to get each device's IP address in Wi-Fi Direct scenario?

查看:1024
本文介绍了如何获得了Wi-Fi直连方案中的每个设备的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ICS开始,Wi-Fi直介绍。通常情况下,我们使用了 WifiP2pManager 类的Wi-Fi Direct的工作,但它似乎只能检索之后连接的GroupOwner IP地址。但是,实际上,在任何设备上的所有前来洽谈,成为GroupOwner。在上层应用程序,我们需要得到对方的IP地址,或者各端的IP地址在一组,这样我们可以发送/与他们沟通。

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.

如何获得了Wi-Fi直连的每个IP地址?包括自己的IP地址,组中的每个同行?

How to get each IP address in Wi-Fi Direct? Include own IP address and each peer in the group?

推荐答案

你可以得到最好的答案可能是从马诺一个:

The best answer you can get is possibly the one from Mano:

我遇到同样的问题。由于这两种设备知道组   拥有者的IP,这已经是可以将消息发送到该组   所有者。您发送的第一个消息可以包含的IP地址   其他设备;从那时起,双向通信是可能的。

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.

下面是我是如何实现它。当我一个客户端连接到通过WiFi直接论坛版主,我得到的组所有者的IP地址,并发送消息到组所有者通过套接字。是这样的:

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

您已经知道的 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());
}

这是实际的code我使用。我将用一些有用的信息替换此消息,像包含发件人的MAC消息对象,它可以用来了解MAC-IP关系,因为 WifiP2pDevice 仅提供MAC和< STRONG>的InetAddress 的IP(有谁知道,如果有一种方法来从的InetAddress 对象获取的MAC?)

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?)

这篇关于如何获得了Wi-Fi直连方案中的每个设备的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆