如何在Wifi-Direct Android中设置特定群组所有者

8

我有一个WiFi Direct的Android应用程序,将在两个手机上运行。

手机1连接到手机2时,我希望手机1充当客户端手机2充当服务器

我使用了以下代码:

if (info.groupFormed && info.isGroupOwner) {

           // start the server thread

   } else if (info.groupFormed) {

            // start the client thread
   }

但问题是,有时候发起连接的phone1我希望它充当客户端,但它有时会充当GroupOwner,并且服务器线程在客户端手机上启动。
我想确保phone2始终充当GroupOwnerserver
2个回答

13

要将对等方设置为客户端而不是每次都是 GroupOWner,我们需要分配:

config.groupOwnerIntent = 0;

那反过来呢?如何强制一个对等方不成为客户端? - J.R.
2
它取决于值的范围,该范围在0-15之间,值越高,成为组所有者的可能性就越大。 - Hammad Shahid

5

好的,你可以做的是:

@Override
public void connect(final WifiP2pDevice device) {
    //obtain a peer from the WifiP2pDeviceList
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    config.groupOwnerIntent = 15; // I want this device to become the owner
    mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            //success logic
            Log.d(P2P, "connected to device " + device.deviceName);
        }

        @Override
        public void onFailure(int reason) {
            //failure logic
            Log.d(P2P, "unable to establish connection to device " + device.deviceName);
        }
    });
}

请注意config.groupOwnerIntent = 15;这一行。它告诉WifiP2pConfig我们发起连接的设备更有可能成为GroupOwner。

文档在这里

希望对您有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接