简体   繁体   English

使用WiFi Direct连接Android设备

[英]Connecting android device using WiFi Direct

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. 但是我不知道Wifip2pManager类的connect和createGroup函数之间的区别。 What's the core difference between them, Please help! 它们之间的核心区别是什么,请帮忙!

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: 不需要createGroup,您只需要以这种方式调用connect方法:

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; 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. groupOwnerIntent优先考虑我们自己的设备优先级,以使其不再成为groupOwner。 Rest is upto you how you want your device to behave. 其余一切取决于您希望设备如何运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM