2013-05-04 27 views
3

我想通過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> 

感謝您的幫助。

回答

3

要發送數據,您需要知道接收器的IP地址(而不是設備地址)。對於P2P客戶端,IP地址group_ownerWifiP2pInfo變量中可用,因此它可以使用它將數據發送給組所有者。如果組主人知道它想要發送數據的P2P客戶端的IP地址,那麼它也可以發送文件。這可以通過兩種方式來實現。

  1. 組所有者爲客戶端分配IP地址並存儲關於它的信息。
  2. 每個新添加的客戶端在加入組時都會將其IP地址發送給組所有者。
+0

究竟是什麼能去嗎? – Acheese 2013-06-20 07:08:13

+1

GO表示擁有該組所有憑據的組所有者.GO持續指引任何新設備發現它。願意加入組的所有新設備都應經過與組所有者(GO)的身份驗證和關聯過程。 – Chait 2013-06-24 11:02:13

+0

你如何將你的設備的IP地址發送到GO? – AndroGeek 2013-07-16 12:50:03

3

您可以通過反射刪除所有組,但是,這是一個黑客,類成員的位可能會改變以後

private void deletePersistentInfo() { 
    try { 

     Class persistentInterface = null; 

     //Iterate and get class PersistentGroupInfoListener 
     for (Class<?> classR : WifiP2pManager.class.getDeclaredClasses()) { 
      if (classR.getName().contains("PersistentGroupInfoListener")) { 
       persistentInterface = classR; 
       break; 
      } 

     } 

     final Method deletePersistentGroupMethod = WifiP2pManager.class.getDeclaredMethod("deletePersistentGroup", new Class[]{Channel.class, int.class, ActionListener.class}); 




     //anonymous class to implement PersistentGroupInfoListener which has a method, onPersistentGroupInfoAvailable 
     Object persitentInterfaceObject = 
       java.lang.reflect.Proxy.newProxyInstance(persistentInterface.getClassLoader(), 
         new java.lang.Class[]{persistentInterface}, 
         new java.lang.reflect.InvocationHandler() { 
          @Override 
          public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable { 
           String method_name = method.getName(); 

           if (method_name.equals("onPersistentGroupInfoAvailable")) { 
            Class wifiP2pGroupListClass = Class.forName("android.net.wifi.p2p.WifiP2pGroupList"); 
            Object wifiP2pGroupListObject = wifiP2pGroupListClass.cast(args[0]); 

            Collection<WifiP2pGroup> wifiP2pGroupList = (Collection<WifiP2pGroup>) wifiP2pGroupListClass.getMethod("getGroupList", null).invoke(wifiP2pGroupListObject, null); 
            for (WifiP2pGroup group : wifiP2pGroupList) { 
             deletePersistentGroupMethod.invoke(wifiP2pManager, channel, (Integer) WifiP2pGroup.class.getMethod("getNetworkId").invoke(group, null), new ActionListener() { 
              @Override 
              public void onSuccess() { 
               //All groups deleted 
              } 

              @Override 
              public void onFailure(int i) { 

              } 
             }); 
            } 
           } 

           return null; 
          } 
         }); 

     Method requestPersistentGroupMethod = 
       WifiP2pManager.class.getDeclaredMethod("requestPersistentGroupInfo", new Class[]{Channel.class, persistentInterface}); 

     requestPersistentGroupMethod.invoke(wifiP2pManager, channel, persitentInterfaceObject); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
}