3

To try out the nearbyAPI, I decided to build an app that would allow a teacher to track attendance of student in a class. Similar to what Caren Chang is doing.

I have gone through the google sample codes on rockpaperscissors and walkietalkie

But only a single device seems to connect at any one time when i test the samples using 4 phones.

I want to build a teacher and student app which the teacher advertises and discovers students, sends payloads to each and every connected device simultaneously as more devices become connected in a classroom set up.

How can i use nearby api to connect and send data to new and multiple devices simultaneously?

Job M
  • 3,331
  • 2
  • 19
  • 26

1 Answers1

6

That's a great use case, and one we've talked about in the past.

If it's specifically for attendance, then you don't need to form a connection. You can have each device advertise while one device constantly scans. You'll build up a list of devices quickly that way.

If you want to do more than attendance, though, such as pushing an assignment to everyone's device, you'll need to build a mesh. To start with, you'll want to use Strategy.P2P_CLUSTER. We have 3 strategies available inside Nearby Connections (CLUSTER, STAR, POINT_TO_POINT) and cluster is the most general one. With cluster, you can connect to as many devices as you want, and you can receive incoming connections from as many devices as you want. Or, almost... The Bluetooth radio inside phones is weak and can only hold 3~4 connections at a time.

To be able to connect all ~30 devices, I'd recommend forming a 'snake-like' connection. The head and tail of the device will scan and advertise at the same time (and devices that aren't connected to anyone are considered snakes of length 1). The heads and tails will keep connecting to each other (being sure not to connect to itself*), and you'll pretty quickly have a long chain of connections connecting everyone together. From there, you can forward messages down the chain to make sure everyone gets it.

  • To avoid connecting to yourself, you can either assign every device a random number (eg. 1, 4, 8, 10) and each device tries to connect to the next highest number, or you can broadcast a message when connecting and disconnect if you get an echo back (because the broadcast went in a circle).
Xlythe
  • 1,958
  • 1
  • 8
  • 12
  • 1
    This is really good, how can **i achieve this in code**. especially forming _a 'snake-like' connection_. Have searched online for sample codes on mesh and P2P_CLUSTER but theirs little information to work with. **Do you have any resources you can point me to?** – Job M Oct 13 '18 at 11:58
  • 1
    We have a sample app internally that does this. I'll ask around to see why it hasn't been published yet. – Xlythe Oct 14 '18 at 17:33
  • 1
    That will be awesome, when **its ready, drop the link**. _If i understand it_ lol , I'll do a blog :). – Job M Oct 15 '18 at 16:00
  • Did the sample get published ? – Job M Nov 05 '18 at 21:09
  • Not yet, sorry. – Xlythe Nov 07 '18 at 17:48
  • Hey @Xlythe my email is getabujob@gmail.com. If i could get sample code it would help me big time. Have spent a month writing spaghetti snake code :(. – Job M Nov 11 '18 at 23:34
  • Sorry, I've been drowning in work and won't be able to publish the sample any time soon. I can't simply email it, since there's a release process we have to go through at Google. :( – Xlythe Jan 30 '19 at 19:22
  • 3
    Hello guys, finally I have made a demo app which follows that "snake-like" connection. Here is the link of the repo: https://github.com/nSpider/NearbyConnectionMeshNetwork. NOTE: The source code is not well commented or organized. Feel free to have a look and criticize. :P Hope it helps – Dipayan Ray Feb 10 '19 at 16:06