如何使用wifip2pmanager android使设备A客户端和设备B服务器反之亦然

时间:2019-08-26 03:21:57

标签: android wifip2p

我正在使用wifip2p创建共享应用,并且我已经编写了在设备之间共享文件的代码,它可以正常工作,但是出现了问题,即设备A变成了 每次都是收件人(组所有者)。 我花了10天的时间解决这个问题,但是一直没有奏效。 请帮助我解决这个问题。

//与所需设备连接。

  public void connect(WifiP2pDevice device) {

    //obtain a peer from the WifiP2pDeviceList
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    deviceAddress = device.deviceAddress;
    Log.d(TAG, "connect: " + deviceAddress);
    manager.connect(channel, config, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.d(TAG, "onResume:  " + getIpAddress(getApplicationContext()));
        }
        @Override
        public void onFailure(int reason) {
            Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

//使设备成为发件人和其他收件人。

public void onConnectionInfoAvailable(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)).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);
}

0 个答案:

没有答案