2

I'm trying to use WiFi-Direct for connecting multiple devices over wifi in a master-slave style (one to many)- one client creates a group using the "createGroup" function, and all the other clients should connect to the group (manually). when a client press on a "Discover peers" button, i want to give him a list of all the master peers. And here is the problem- I can't find a way to differentiate between slave peers and the master peer (the one who initiate the createGroup request).

is there any way to filter out all the slave peers and keep only the master peers?

ekad
  • 14,436
  • 26
  • 44
  • 46
niryo
  • 1,275
  • 4
  • 15
  • 26

2 Answers2

3

You should be discovering services rather than peers, though the API does work better if you also do peer discovery, thus here's my proposal for your logic: With slave:

  • Start peerDiscovery
  • When you get Peers changed event, start service discovery (for service_type defined by your master)
  • Add the discovered services into a selection list as they come (note that they come one by one, and I've seen max 5 seconds between discovered services)

With Master

  • createGroup
  • Add local service to advertise that you are the master
  • Start Peer discovery, and make sure by monitoring the Discovery state changes that it stays on (if it goes off, your service advertisement likely will not be seen by the slaves)
Dr.Jukka
  • 2,346
  • 2
  • 15
  • 20
  • Do services are supported only from jelly beans and on? your solution sounds good but i want to wait and see if there will be a more cross versions solution.BTW I don't really understand why do we need the first stage of discovering peers though...what to you mean by "API works better"? – niryo May 15 '15 at 07:15
  • And thanks a lot for you reply, i will tag it as the excepted answer soon if i wan't get a cross version solution. – niryo May 15 '15 at 07:22
  • The "API works better" is somehow explained in my blog : http://www.drjukka.com/blog/wordpress/?p=81 . Basically if you just do the discovery once, its likely fine, but in longer time usage, its likely giving problems. Thus to avoid problems, its advisable to use peers discovery always – Dr.Jukka May 15 '15 at 08:02
  • Things might have been changed during the years since but this advice no longer seems to help. As far as I can tell from actual testing, the system refuses to start the service discovery stating that another discovery is already in progress. – Gábor Dec 22 '21 at 11:24
1

A simple way to achieve this is to do the following: You can set which device to be Group Owner (Master device) by setting the groupOwnerIntent to 15.

WifiP2pConfig config = new WifiP2pConfig();
config.groupOwnerIntent = 15;  //Value between 0-15

You also need to change the master's device name to something like "Master"+itsCurrentName. (To change the WiFi Direct device name, check my answer on how to set interface device name of wifi direct)

Now, whenever a new device scans for peers, the results will show which devices are GroupOwners from their name that starts with the word "Master".

This is a simple way to filter out master from slave peers.

Community
  • 1
  • 1
Ziad Halabi
  • 964
  • 11
  • 31
  • Thanks for your reply! i chose to go with the other solution but this one was also useful for me – niryo May 15 '15 at 11:15