Android Nearby, Connections vs. Messages

Recently I’ve explored on connecting two devices in close proximity without need of using GPS. Android provide an excellent feature for that called Android Nearby. There are 3 variants of it, whereby Nearby Notifications is still are preliminary at the time of writing this. Anyway, Nearby Notification is clearly a beacon based notification, hence not relevant to what I’m looking for.

Hence the other 2, Nearby Connections and Nearby Messages, which to choose? After explored both, I’m listing what I found for the others to check out

Can your connection contain within local area network?

The Nearby Connection is only restrict to local area network (i.e. Wifi or Ethernet). If your application does want to connect across network. You’ll have to op for Nearby Messages. It uses Bluetooth, BLE, Wifi and also Ultrasonic Sound wave!!

Does it need to be cross platform?

The Nearby Connection from what was written, only limited to Android. But for Nearby Messages, there’s a Beta version of support for iOS. It uses the Ultrasonic Sound for the connection. What a neat idea!

The need to be linked using Google API

From the above two consideration, Nearby Messages is definitely more superior than Nearby Connections of it’s support. But one need to register and get an API key to use Nearby Messages. While Nearby Connection is just as simple as a meta-data key that is used to match the both devices will do.

The communication model preferences

Although both are connecting Nearby devices, but the communication model used by Nearby Connections and Nearby Messages is distinctly different.

Nearby Messages

Nearby Messages Communication Model

The Nearby Messages is like a broadcast and receiver model. It’s a many-to-many model, where all devices broadcast and everyone listens. The function used is Nearby.Messages.publish and Nearby.Messages.subscribe.

This doesn’t mean you can’t restrict one-to-one connection, as you could still perform manual filtering of the message received. One will have to manually assign a specific ID to the publishing device for the receiving device to identify which device is it getting the message from. Note that the packets send is received by all, and this is not the natural communication model of Nearby Messages.

Nearby Connections

Nearby Connections Communication Model

Differs from Nearby Messages, the Nearby Connections is more like a Master-Slaves Model (One-to-Many model). The Master Device can Nearby.Connections.startAdvertising to all the devices. The nearby devices could use Nearby.Connections.startDiscovering to discover the advertisement. Upon discovery, the Client device will got the Master Devices endpoint reference, that it could sent back to acknowledge connections, and stop discovering upon that. The Master Device will receive the client endpoints token for it to stay connected.

With that, the Client devices will only be connected to one Master, whichever it first discover (if there’s more than one Master advertising). We can’t force a Client to receive more than one, as we have no control to filter out and connect to the subsequent master in the queue.

Work across Android activities or in Background

Oh… sorry. None of them work across activities, nor in background. Both would need the Activity to be active and in foreground. You could make the connection need reconnect upon exit and entering another activity, but that would make the connection flaky and the code messier. It’s an explicit model by Google to prevent such connection to go across activities nor background for security reasons I believe. Note there’s also a “connection permission” also surface to the user when the connection is starting. So upon exiting your activity, the connection will get terminate.

Use Fragment, or Views visibility to control multiple views and actions if various scenario needed the connections.


If we look at overall, I think Nearby Messages is more superior than Nearby Connections if you are not sure which to choose from. But I still hope Google will have something that allow us to connect and communicate across the activity or at background, without the need of the user keep the device on if when the Nearby is being used.