Android的的Wi-Fi直连网络 [英] Android Wi-Fi Direct Network

查看:144
本文介绍了Android的的Wi-Fi直连网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在Android上,我正在寻找范围内的所有的同行和后来跟所有的应用程序,是谁发起的设备的发现成为集团的所有者和所有其他成为客户,我所做的一切连接东西,但现在我想的组所有者将消息发送给所有连接的同行,如何实现这一点,也请告诉我什么是在对等通信的方法,是否P2P在Android中也使用IP发送和接收数据?

I am developing an application on Android where I am searching for all the peers in the range and afterwards connect with all of them, The device who initiated the the discovery become the group owner and all others become client, I have done all the connection thing but now I want to the group owner to send the message to all the connecting peers, How to achieve this and also please tell me what is the methodology in peer-to-peer communication , Does p2p in Android also use IP to send and receive data?

三江源 商祺利布。

推荐答案

Wi-Fi直/ P2P可以被认为是正常的无线网络连接,但本集团所有者(GO)作为一个软件接入点(DHCP服务器,配置, 等等)。因此,要回答你的最后一个问题,是Wi-Fi直还使用IP来发送和接收数据。

Wi-Fi Direct/P2P can be considered as normal Wi-Fi but where the group owner (GO) acts as a software access point (dhcp server, provisioning, etc). So to answer your last question, yes Wi-Fi Direct also uses IP to send and receive data.

您想将数据发送到组中的所有成员?有两种解决方案,这一点:

You want to send data to all members in the group? There are two solutions for this:

  1. 广播使用多播一次的消息。
  2. 发送消息到组中的每个individuel客户端。

最有效的方法将是解决办法1,使用多播广播数据,因为你只需要一次数据发送。不幸的是无线网络的组播支持非常分散在Android中,由于大量的设备,似乎阻断非单播流量。如果要沿着这条路走下去,请参阅这篇文章进行更深入的信息。

The most efficient method would be solution 1, to broadcast the data using multicast, as you would only need to send the data once. Unfortunately Wi-Fi multicast support is very fragmented in Android, as a lot of devices seem to block non-unicast traffic. See this article for more in depth information if you want to go down this route.

解决方案2是最好的方法,如果你想保证支持所有设备上,只发送少量数据。在GO需要组中的客户端的IP地址,但由于Wi-Fi直在Android的实现方式,只有GO IP是众所周知的所有设备。一种解决方案是让客户端连接到插座上的执行,以获得他们的IP地址:

Solution 2 is the best method if you want to guarantee support on all devices and only transmit a small amount of data. The GO need the IP addresses of the clients in the group, but because of the way Wi-Fi Direct is implemented in Android, only the GO IP is known to all devices. One solution is to let the clients connect to a socket on the GO, to get their IP address:

客户端code

private static final int SERVER_PORT = 1030;

... // on group join:
wifiP2pManager.requestConnectionInfo(channel, new ConnectionInfoListener() {
    @Override
    public void onConnectionInfoAvailable(WifiP2pInfo p2pInfo) {
        if (!p2pInfo.isGroupOwner) {
            // Joined group as client - connect to GO
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(p2pInfo.groupOwnerAddress, SERVER_PORT));
        }
    }
});

集团老板code:

Group owner code:

private static final int SERVER_PORT = 1030;
private ArrayList<InetAddress> clients = new ArrayList<InetAddress>();

public void startServer() {
    clients.clear();
    ServerSocket serverSocket = new ServerSocket(SERVER_PORT);

    // Collect client ip's
    while(true) {
       Socket clientSocket = serverSocket.accept();
       clients.add(clientSocket.getInetAddress());
       clientSocket.close();
    }
}

现在你需要做的就是启动每个客户端上一个ServerSocket,并通过客户端列表,进入循环创建一个套接字连接到每发送要播出的消息。

Now all you need to do is start a serversocket on each client, and make to GO iterate through the client list creating a socket connection to each and sending the message you want to broadcast.

这篇关于Android的的Wi-Fi直连网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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