如果他按下连接按钮,如何使设备成为客户端或发送者

时间:2019-08-23 12:47:19

标签: java android wifi-direct

我正在使用wifip2p manager将文件从一台设备发送到另一台设备。 总是同一台设备成为客户端,或者换句话说,另一台设备始终是组所有者,他们将始终收到data

我尝试使用下面的代码来发送和接收data

void onConnectionInfoAvailable(WifiP2pInfo info) {

        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)).execute();
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case, we enable the
            // get file button.
            mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
            ((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources().getString(R.string.client_text));
        }
        // hide the connect button
        mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);
    }

1 个答案:

答案 0 :(得分:0)

这些是您需要检查如何实施解决方案的资源:

https://android.googlesource.com/platform/frameworks/base/+/969c243/docs/html/training/connect-devices-wirelessly/wifi-direct.jd

https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo

请紧记:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

(需要许可才能找到附近的同伴)

https://developer.android.com/reference/android/net/wifi/WpsInfo.html#PBC

WpsInfo.PBC仅在api 28之后才被弃用,因此您需要将其设置为WifiP2pConfig实例才能连接到通道