我想通過Wi-Fi Direct在兩臺設備之間傳輸文件。Wi-Fi Direct Android
我想做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>
感謝您的幫助。
究竟是什麼能去嗎? – Acheese 2013-06-20 07:08:13
GO表示擁有該組所有憑據的組所有者.GO持續指引任何新設備發現它。願意加入組的所有新設備都應經過與組所有者(GO)的身份驗證和關聯過程。 – Chait 2013-06-24 11:02:13
你如何將你的設備的IP地址發送到GO? – AndroGeek 2013-07-16 12:50:03