2016-07-04 133 views
2

我正在使用Android Wi-Fi Direct服務發現功能。當連接信息可用時,如果組形成,我請求組信息。出於某種原因,當我作爲客戶端連接時,我可以從組中獲取組所有者,但客戶端列表爲空。當我作爲羣組所有者進行連接時,我可以獲得羣組所有者和客戶名單。當我到達聊天片段時,我試圖顯示組所有者和客戶是誰。另一個問題是,組所有者設備名稱似乎是空白的,但其餘的信息是正確的,如地址(我刪除了地址行)。我嘗試了在幾個不同的地方請求羣組信息,並且當我作爲客戶端連接時,我永遠無法獲得客戶端列表。當我收到SERVICE_CONNECTED意圖時,我切換到聊天片段。當我作爲客戶端連接時,爲什麼客戶端列表爲空?

連接的客戶端:

I/wfd_: Wi-Fi P2P Connection Changed 
I/wfd_: Connected to P2P network. Requesting connection info 
I/wfd_: Connection info available 
I/wfd_: WifiP2pInfo: 
I/wfd_: Group formed: true 
     Is group owner: false 
     Group owner address: /192.168.49.1 
I/wfd_: Stopping service discovery 
I/wfd_: Service discovery stopped 
I/wfd_: Connected as client 
I/wfd_: Requesting group info 
I/wfd_CommReceiver: This device changed 
I/wfd_ClientSocketHandler: Client socket thread running 
I/wfd_ClientSocketHandler: Opening client socket 
I/System.out: [CDS][DNS] getAllByNameImpl netId = 0 
I/System.out: [socket][0] connection /192.168.49.1:4545;LocalPort=34789(5000) 
I/System.out: [CDS]connect[/192.168.49.1:4545] tm:5 
I/wfd_: Service discovery requests cleared 
I/wfd_: Group info available 
I/wfd_: WifiP2pGroup: 
I/wfd_: Network name: DIRECT-Ig-BLU Clark 2 
     Is group owner: false 
     Group owner: 
     Device name: BLU Clark 2 
     Status: Unavailable 

     Client list is empty. 
/System.out: [socket][/192.168.49.117:34789] connected 
I/wfd_ClientSocketHandler: Client socket - true 
I/wfd_ClientSocketHandler: Launching the I/O handler 
I/wfd_CommReceiver: Service connected 
I/wfd_CommReceiver: Switching to Chat fragment 
I/wfd_: handleMessage() called 

連接爲組擁有者:

I/wfd_: Wi-Fi P2P Connection Changed 
I/wfd_: Connected to P2P network. Requesting connection info 
I/wfd_: Connection info available 
I/wfd_: WifiP2pInfo: 
I/wfd_: Group formed: true 
     Is group owner: true 
     Group owner address: /192.168.49.1 
I/wfd_: Stopping service discovery 
I/wfd_: Service discovery stopped 
I/wfd_: Connected as group owner 
I/wfd_OwnerSocketHandler: Group owner server socket started 
I/wfd_: Requesting group info 
I/wfd_OwnerSocketHandler: Group owner server socket thread running 
I/wfd_: Service discovery requests cleared 
I/wfd_CommReceiver: This device changed 
I/wfd_: Group info available 
I/wfd_: WifiP2pGroup: 
I/wfd_: Network name: DIRECT-XB-Brendan BLU 
     Is group owner: true 
     Group owner: 
     Device name: 
     Status: Unavailable 

     Client: 
     Device name: BLU Clark 2 
     Status: Connected 
I/wfd_CommReceiver: Service connected 
I/wfd_CommReceiver: Switching to Chat fragment 
I/wfd_OwnerSocketHandler: Launching the I/O handler 
I/wfd_: handleMessage() called 

代碼:

@Override 
    public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) { 
     Log.i(TAG, "Connection info available"); 

     Log.i(TAG, "WifiP2pInfo: "); 
     Log.i(TAG, p2pInfoToString(wifiP2pInfo)); 
     this.groupFormed = wifiP2pInfo.groupFormed; 
     this.isGroupOwner = wifiP2pInfo.isGroupOwner; 

     if (wifiP2pInfo.groupFormed) { 
      stopServiceDiscovery(); 

      Thread handler; 
      if (wifiP2pInfo.isGroupOwner) { 
       Log.i(TAG, "Connected as group owner"); 
       try { 
        handler = new OwnerSocketHandler(this.getHandler()); 
        handler.start(); 
       } catch (IOException e) { 
        Log.e(TAG, "Failed to create a server thread - " + e.getMessage()); 
        return; 
       } 
      } else { 
       Log.i(TAG, "Connected as client"); 
       handler = new ClientSocketHandler(this.getHandler(), wifiP2pInfo.groupOwnerAddress); 
       handler.start(); 
      } 

      Log.i(TAG, "Requesting group info"); 
      // Requests peer-to-peer group information 
      wifiP2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() { 
       @Override 
       public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) { 
        Log.i(TAG, "Group info available"); 
        if (wifiP2pGroup != null) { 
         Log.i(TAG, "WifiP2pGroup:"); 
         Log.i(TAG, p2pGroupToString(wifiP2pGroup)); 
         WifiDirectHandler.this.wifiP2pGroup = wifiP2pGroup; 
        } else { 
         Log.w(TAG, "Group is null"); 
        } 
        localBroadcastManager.sendBroadcast(new Intent(Action.SERVICE_CONNECTED)); 
       } 
      }); 

     } else { 
      Log.w(TAG, "Group not formed"); 
     } 
     localBroadcastManager.sendBroadcast(new Intent(Action.DEVICE_CHANGED)); 
    } 

回答

相關問題