Android Wifip2p:为什么连接到组所有者后组信息为空

Android Wifip2p:为什么连接到组所有者后组信息为空,android,sockets,wifi-direct,wifip2p,Android,Sockets,Wifi Direct,Wifip2p,我正在尝试将多个设备连接到我手动选择的组所有者 我希望对等方在找到组所有者后手动连接到组所有者 我有3部手机(没有模拟器),每部手机上都有一个带有点击处理程序的“创建组”按钮 public void createWifiGroup(View view) { mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() { @Override public void onSuccess

我正在尝试将多个设备连接到我手动选择的组所有者

我希望对等方在找到组所有者后手动连接到组所有者

我有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
所以我想它成功了

我还有一个
MyPeerListener
类,当收到
WIFI\u P2P\u PEERS\u CHANGED\u ACTION
操作意图时,
BroadCastReceiver
调用该类

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));
        }

        }


    }
对requestGroupInfo的调用还显示:

group is null
这种情况在80%的情况下经常发生。有时它显示默认组IP
192.168.49.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()
onSuccess()回调中删除了
requestConnectionInfo()
,并在扩展
BroadcastReceiver的
MyWifiDirectBroadcastReceiver
中取出它

@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
此更改后不会返回空信息,我连接到侦听器提供的IP时没有任何问题。

Wi-Fi Direct存在许多奇怪的问题。我用它工作了一段时间,列出了一些你可能会觉得有用的资源和问题:@Brendan的确,有时它看起来很不一致。我发现,当连接类似的设备时,并且附近几乎没有网络活动时,它工作得最好。如果您正在学习,您可能还会发现此Wi-Fi Direct handler库非常有用:它记录了大量信息,您可以查看源代码,了解它如何管理组连接
@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
    }
}