2016-11-18 51 views
4

我试图将多个设备连接到一组所有者我手动选择的Android Wifip2p:为什么是组信息空连接到组所有者

我希望同龄人连接到群主后,手动,一旦他们找到他

我有3个手机(没有模拟器),每个没有与此单击处理

public void createWifiGroup(View view) { 

    mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() { 
     @Override 
     public void onSuccess() { 

      mManager.requestGroupInfo(mChannel,new MyGroupInfoListener(MainActivity.this)); 

     } 

     @Override 
     public void onFailure(int reason) { 

     } 
    }); 

} 

了“创建组”按钮,你可以看到我做requestGroupInfo并将它传递是打印的监听器在日志中输入以下行:

groupFormed: true isGroupOwner: true groupOwnerAddress: /192.168.49.1 

,所以我想它成功

我也有收到WIFI_P2P_PEERS_CHANGED_ACTION行动的意图时BroadCastReceiver呼叫MyPeerListener类。

MyPeerListener将遍历的同行和连接,如果它发现群主

@Override 
    public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) { 
     this.wifiP2pDeviceList = wifiP2pDeviceList; 
     //Toast toast = Toast.makeText(receiver.mActivity, "I found some peers", Toast.LENGTH_LONG); 
     // toast.show(); 
     Iterator<WifiP2pDevice> deviceListIterator = wifiP2pDeviceList.getDeviceList().iterator(); 
     boolean foundGroup = false; 
     while (deviceListIterator.hasNext()) { 
      final WifiP2pDevice device = deviceListIterator.next(); 
      if (!foundGroup && device.isGroupOwner() && !MainActivity.connected) { 

       //MainActivity.mManager.removeGroup(MainActivity.mChannel, null); 

       foundGroup = true; 

       final WifiP2pConfig config = new WifiP2pConfig(); 
       //config.wps.setup = WpsInfo.PBC; 
       config.groupOwnerIntent = 0; 
       config.deviceAddress = device.deviceAddress; 

       MainActivity.mManager.connect(MainActivity.mChannel, config, new WifiP2pManager.ActionListener() { 

        @Override 
        public void onSuccess() { 
         //success logic 
         Toast toast = Toast.makeText(receiver.mActivity, "connect success", Toast.LENGTH_SHORT); 
         toast.show(); 
         MainActivity.connected = true; 
         MainActivity.mManager.requestGroupInfo(MainActivity.mChannel, new MyGroupInfoListener(receiver.mActivity)); 
         MainActivity.mManager.requestConnectionInfo(MainActivity.mChannel, new MyConnectionInfoListener(receiver)); 


        } 

        @Override 
        public void onFailure(int reason) { 
         //failure logic 
         //MainActivity.connected = false; 
         Toast toast = Toast.makeText(receiver.mActivity, "connect fail, reason: " + reason, Toast.LENGTH_LONG); 
         toast.show(); 
        } 

       }); 
      } 
      ListView list = (ListView) receiver.mActivity.findViewById(R.id.device_list_view); 
      list.setAdapter(new DeviceListViewAdapter(this, receiver.mActivity)); 
     } 

     } 


    } 

(我知道使用静态变量和整体设计不好的,但是这是一个快速原型学习)

现在,我得到一个connect successToastrequestConnectionInfo右后连接显示我此行

groupFormed: false isGroupOwner: false groupOwnerAddress: null 

到requestGroupInfo的调用也显示:

group is null 

这种情况经常时代的约80%。有时会显示默认组IP 192.168.49.1。但是如果“奴隶”手机都有正确的群组信息,我会很幸运。

当我尝试在组信息为空的情况下将套接字连接到所有者时,它将失败,并返回destination host unreachable。但是当组信息正确时,我可以正确连接和发送数据。

那么为什么会发生这种情况呢?连接成功的可能性如何,但该组为空?

+0

Wi-Fi Direct有很多奇怪的问题。我使用了它一段时间,并列出了一些您可能会觉得有用的资源和问题:https://groups.google.com/forum/#!论坛/ wi-fi-direct – Brendan

+0

@Brendan的确,它在时间上似乎很不一致 – vlatkozelka

+0

我发现它在连接类似设备时效果最好,并且在附近的网络活动很少。如果您正在学习,您可能还会发现这个Wi-Fi Direct处理程序库很有用:https://github.com/Crash-Test-Buddies/WiFi-Buddy它记录了大量信息,您可以查看源代码和了解它如何管理群组连接 – Brendan

回答

1

我重新做了整个项目,发现只有一个错误

我是做

mManager.connect(...,new ActionListener(){ 
@Override 
onSuccess(){ 
    //Here I called requestConnectionInfo right after the pairing happens 
    mManager.requestConnectionInfo(myConnectionInfoListener); 
    Socket socket = new Socket(); 
    // try to connect and send data.... 

} 

@Override 
onFailure(int reason){ 

} 

}); 

所以我删除了从onSuccess()回调connect()的里里外外它requestConnectionInfo()MyWifiDirectBroadcastReceiver扩展BroadcastReceiver

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { 
     // Check to see if Wi-Fi is enabled and notify appropriate activity 
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { 
     // Call WifiP2pManager.requestPeers() to get a list of current peers 
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 
     //Here is where the request for connection info should happen 
     //As sometimes the pairing can happen but the devices 
     //might still be negotiating group owner for example 
     mManager.requestConnectionInfo(myConnectionInfoListener()); 
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { 
     // Respond to this device's wifi state changing 
    } 
} 

MyConnectionInfoListener t之后永不返回空信息他的变化,我连接到听众提供的IP没有任何问题。

相关问题