I want to transfer files between 2 devices via Wi-Fi Direct.
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.
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.
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.
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>
Thanks for help.
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 theWifiP2pInfo
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.You can delete all groups through reflection but, it's bit of a hack and class members might change later