我想通过Wi-Fi Direct在两台设备之间传输文件。
我想做与WifiDirectDemo相同的事情,但无法从群主传输数据到另一台设备,所以我尝试了这个方法:每次其中一个设备点击连接时,另一台设备被设置为群主,因此在每次连接时,请求连接的设备始终是客户端并可以发送数据。
问题在于Android始终记住第一个创建的群组和其群主。换句话说,我所做的只有第一次有效,除非我进入设置并忘记第一个连接创建的群组。
我知道通过使用断开按钮可以删除Wi-Fi组,但Android系统会将其放入已记住的群组中,并在需要进行新连接时使用其设置(群主协商)。
我尝试的第二件事是在每个设备上创建一个ServerSocket
(在另一个端口),这样群主和其他设备都将同时充当客户端和服务器。我不知道群主是否可以被设置为客户端,但我不能在两个设备上都创建ServerSocket
。
以下是我的代码:
<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>
感谢您的帮助。