Question

I am developing an application which first discover the peers in range and then connect with all of them one by one my function look like this:

void connectTo(WifiP2pDevice device) {
        WifiP2pConfig config = new WifiP2pConfig();
        config.deviceAddress = device.deviceAddress;
        config.groupOwnerIntent=15;
        wifiP2pManager.connect(wifiDirectChannel, config, actionListener);
        wifiP2pManager.createGroup(wifiDirectChannel, actionListener);
      }

But I don't know the difference between the connect and createGroup function of Wifip2pManager class. What's the core difference between them, Please help!

Was it helpful?

Solution

I know I am late to answer but I am sure it would help others. There is no need to createGroup, you simply need to call connect method in this way:

void connectTo(WifiP2pDevice device) {
            WifiP2pConfig wifiP2pConfig = new WifiP2pConfig();
            wifiP2pConfig.deviceAddress = device.deviceAddress;
            wifiP2pConfig.groupOwnerIntent = 0;
            wifiP2pConfig.wps.setup = WpsInfo.PBC;

            if (wifiP2pManager != null) {

                wifiP2pManager.connect(mChannel, wifiP2pConfig,
                        new ActionListener() {

                            @Override
                            public void onSuccess() {
                                // WiFiDirectBroadcastReceiver will notify us.
                                // Ignore for now.
                                Utility.showToast(
                                        WifiP2PConnectionActivity.this,
                                        Constants.CONNECTED);

                            }

                            @Override
                            public void onFailure(int reason) {
                                Utility.showToast(
                                        WifiP2PConnectionActivity.this,
                                        getErrorMessage(reason));

                            }
                        });
}

It will get connected now. wifiP2pConfig.groupOwnerIntent = 0; is set to zero so that you allow other device to become owner and your own device as client everytime. groupOwnerIntent prioritices our own device priority to be lesser of becoming groupOwner. Rest is upto you how you want your device to behave.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top