如何在Wifi-Direct Android中创建特定的组所有者

Ham*_*hid 4 android wifi-direct

我有一个Wifi Direct Android应用程序,它将在两部手机上运行.

phone1连接到phone2,我想phone1充当一个clientphone2充当一个server.

我用过这段代码:

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

           // start the server thread

   } else if (info.groupFormed) {

            // start the client thread
   }
Run Code Online (Sandbox Code Playgroud)

但问题是,有时phone1哪个已经启动了连接,我希望它充当客户端,它有时充当一个GroupOwner,并且服务器线程在客户端电话上启动.

我想确保phone2始终充当a GroupOwner和a server.

Ham*_*hid 9

GroupOWner将对等体设置为客户端而不是每次,我们必须分配:

config.groupOwnerIntent = 0;
Run Code Online (Sandbox Code Playgroud)

  • 它取决于0-15之间的值,越高的值越高成为groupOwner的可能性. (2认同)

Fel*_*nde 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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

文档在这里

希望能帮助到你。