Wi-Fi Direct安卓 [英] Wi-Fi Direct Android

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

问题描述

我想通过Wi-Fi Direct在2个设备之间传输文件.

I want to transfer files between 2 devices via Wi-Fi Direct.

我想做的事情与WifiDirectDemo中的相同,但是我无法将数据从组所有者传输到另一台设备,因此我尝试了此操作:每次当其中一台设备单击连接时,都会设置另一台设备作为组所有者,因此在每个连接上,要求连接的设备始终是客户端,并且可以发送数据.

I wanted to do the same thing as in WifiDirectDemo, but I can't transfer data from the group owner to the other device, so I tried this: each time when one of the devices clicks connect, the other device is set as the group owner, so on each connection the device who asks for connection is always the client and can send data.

问题在于,Android始终会记住创建的第一个组,因此也要记住其组所有者.换句话说,除非我进入设置并忘记由第一个连接创建的组,否则我只会在第一次工作.

The problem with this is that Android always remembers the first group created and therefore its group owner. In other words, what I did only works the first time unless I go to settings and forget the group created by the first connection.

我知道,通过使用断开连接按钮,Wi-Fi组将被删除,但是Android系统将其放在记住的组中,并在进行新连接时使用其设置(组所有者协商).

I know that by using the disconnect button, the Wi-Fi group is removed, but the Android system puts it in remembered groups and uses its setting (group owner negotiation) when a new connection is to be made.

我尝试的第二件事是在每个设备上(在另一个端口上)创建一个ServerSocket,因此,组所有者和另一个设备将同时是客户端和服务器.我不知道是否可以将组所有者设置为客户端,但是我不能在两个设备上都创建一个ServerSocket.这是我的代码:

The second thing I tried was to create a ServerSocket on each device (on another port), so this way both the group owner and the other device would be clients and servers at the same time. I don't know if the group owner can be set as a client, but I cant create a ServerSocket on both devices. Here is my code:

<pre>
  @Override
    public void onConnectionInfoAvailable(final WifiP2pInfo info) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        this.info = info;
        this.getView().setVisibility(View.VISIBLE);

        // The owner IP is now known.
        TextView view = (TextView) mContentView.findViewById(R.id.group_owner);
        view.setText( getResources().getString(R.string.group_owner_text)
                + ((info.isGroupOwner == true) ? getResources().getString(R.string.yes)
                        : getResources().getString(R.string.no)));

        // InetAddress from WifiP2pInfo struct.
        view = (TextView) mContentView.findViewById(R.id.device_info);
        view.setText("Group Owner IP - " + info.groupOwnerAddress.getHostAddress());


        // After the group negotiation, we assign the group owner as the file
        // server. The file server is single threaded, single connection server
        // socket.
        if (info.groupFormed && info.isGroupOwner) {
            new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text),8988)
                    .execute();
            mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
            Log.d(WiFiDirectActivity.TAG, "serveur8988cree");
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case, we enable the
            // Get file button.
            // In this case we create a server socket on another port
            new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text),8987)
            .execute();
            mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
            Log.d(WiFiDirectActivity.TAG, "serveur8987cree");
            ((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources()
                    .getString(R.string.client_text));
        }
</pre>

感谢您的帮助.

推荐答案

要发送数据,您需要知道接收器的IP地址(而不是设备地址).对于P2P客户端,WifiP2pInfo的IP地址在WifiP2pInfo变量中可用,因此它可以使用该地址将数据发送到组所有者.如果组所有者知道要向其发送数据的P2P客户端的IP地址,则它也可以发送文件.这可以通过两种方式实现.

To send data you need to know the IP address (not the device address) of the receiver. For the P2P client, the IP address of group_owner is available in the WifiP2pInfo variable, so it can use this to send data to the group owner. If the group owner knows the IP address of the P2P client to which it wants to send data, then it can also send files. This can be achieved in two ways.

  1. 组所有者将IP地址分配给客户端并存储有关其的信息.
  2. 每个新添加的客户端在加入群组时都会将其IP地址发送给群组所有者.

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

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