Question

I have developed a WiFi Direct Application and I used this code to differentiate between a groupOwner and other device. But groupOwner is always made randomly. I want to make sure that connecting device acts like a groupOwner every time a connection is made. My code :

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

     // GroupOwner     

   } else if (info.groupFormed) {


   } 
Était-ce utile?

La solution 2

You have to use the property groupOwnerIntent of WifiP2pConfig object , which you pass to the connect() call. For example:

config.groupOwnerIntent = 15;

Value ranges between 0-15 , higher the value higher the possibility of becoming a groupOwner.

Autres conseils

If your goal is to make the device, which is advertising a service, also the group owner, then on your WifiP2pManager instance call createGroup in the onSuccess callback of addLocalService

WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
WifiP2pManager.Channel channel = serverManager.initialize(this, getMainLooper(), null);

HashMap<String, String> record = new HashMap<String, String>();

WifiP2pServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_hello", "_world._tcp", record);

//remove legacy group
manager.removeGroup(channel, null);

//advertise your service
manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() {

  @Override
  public void onFailure(int reason) {
  }

  @Override
  public void onSuccess() {
    //create group, making this device the owner of the group
    manager.createGroup(channel, null);
  }

});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top