Wi-Fi Direct 経由で 2 つのデバイス間でファイルを転送したいと考えています。
WifiDirectDemo と同じことをしたかったのですが、グループの所有者から他のデバイスにデータを転送できないので、これを試しました: デバイスの 1 つが接続をクリックするたびに、他のデバイスがグループとして設定されます。したがって、各接続で接続を要求するデバイスは常にクライアントであり、データを送信できます。
これに関する問題は、Android が最初に作成されたグループを常に記憶しているため、そのグループ所有者であることです。つまり、設定に移動して最初の接続で作成されたグループを忘れない限り、私が行ったことは初めて機能します。
切断ボタンを使用すると Wi-Fi グループが削除されることはわかっていますが、Android システムはそれを記憶されたグループに入れ、新しい接続が確立されるときにその設定 (グループ所有者のネゴシエーション) を使用します。
2 番目に試みたのは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>
手伝ってくれてありがとう。