Android Wi-Fi 直连网络

Android Wi-Fi Direct Network(Android Wi-Fi 直连网络)

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

问题描述

我正在 Android 上开发一个应用程序,我正在搜索范围内的所有对等点,然后与所有对等点连接,发起发现的设备成为组所有者,所有其他设备成为客户端,我已经完成了所有连接的事情,但现在我想让组所有者将消息发送给所有连接的对等点,如何实现这一点,还请告诉我点对点通信的方法是什么,Android 中的 p2p 是否也使用 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 Direct/P2P 可以视为普通 Wi-Fi,但组所有者 (GO) 充当软件访问点(dhcp 服务器、配置, 等等).所以回答你的最后一个问题,是的,Wi-Fi Direct 也使用 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. 将消息发送给组中的每个客户.

最有效的方法是解决方案 1,使用多播广播数据,因为您只需要发送一次数据.不幸的是,Android 中的 Wi-Fi 多​​播支持非常分散,因为许多设备似乎阻止了非单播流量.如果您想沿着这条路线走,请参阅这篇文章了解更多深入信息.

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 地址,但由于 Android 中 Wi-Fi Direct 的实现方式,所有设备仅知道 GO IP.一种解决方案是让客户端连接到 GO 上的套接字,以获取其 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:

客户端代码

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

群主代码:

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,并让 GO 遍历客户端列表,为每个客户端创建一个套接字连接并发送您要广播的消息.

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 直连网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android Wi-Fi 直连网络

基础教程推荐