Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a device groupOwner in WifiDirect?

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) {


   } 

1 Answers

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);
  }

});
like image 82
user2941476 Avatar answered Feb 06 '26 07:02

user2941476



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!