Say hello to devices near you

After a decade of the smartphone era, we are equipped with various means of communication. We have Facebook, Instagram, text messages and simple phone calls for distance communication, and NFC, barcode/QR code scanning, or even IR blasters for the close-range one. And what if we want to communicate with the devices in near vicinity, for example, to easily share a file with everyone in the room during a meeting? This is where Nearby Messages comes into the picture.

What is Nearby Messages, actually?

Google Nearby, first presented in 2015, is a service which allows for exchanging data between the devices in close proximity. What’s best about it? Unlike most of the other communication solutions, Nearby Messages doesn’t require you to install any app, create yet another account, share your phone number, and so on. It works in the publish-subscribe model and it covers up to the Bluetooth range (I managed to use it with the devices that were 20-ish meters apart). It combines Bluetooth, Bluetooth Low Energy, WiFi, and… ultrasounds (how cool is that?) to establish and send data. It’s available for both Android and iOS.

How does it work?

The whole idea might sound like magic, but in reality, it’s very simple. Let’s consider this example: there are two devices, A and B. How do they communicate using Nearby Messages? Here’s what the whole process looks like in general:

Device A (the publisher) makes a request to the server to assign a token to the message that the device wants to send. Then, the publisher makes the token detectable by the devices nearby.

Device B (the receiver) pairs its subscription with a token. Then, the receiver sends its token to the publisher and detects the publisher’s token.

When both devices detect the same token, the message is sent from A to B.

What is important, the Messages are not exchanged directly between devices, but they are sent through Google servers. When one device detects another, it turns to the Google Cloud and says, “Hey! I found a device with this particular token, please give me the message that is associated with it”. Internet connection is required on both devices in order for Nearby Messages to work.

Moreover, Nearby Messages is meant to be used to exchange small packets of data (up to 100 kB). Also, all messages are public (like a broadcast) – in other words, the publisher cannot tell the server, “This device should be able to see me, and that one should not”.

There is a way, however, to limit the range of your messages: you can use just ultrasounds for the broadcast process so that the messages are available only to the devices within a couple of meters from the publisher.

“Show me the code!”

Enough theory, onto the practice! Here’s a simple application which showcases Nearby Messages, so let’s analyze it (you can find its full source code on GitHub).

To use the Nearby Messages API, you will need an API key. To get one, follow the instructions here (Step 3: Get API key).

Google made this API very easy to integrate with Android applications – it takes just about 10 lines of code:

Note! When Nearby Messages is turned on (subscribed and/or publishing) it has a big impact on you battery so make sure to turn it off when you are done with using it.

Listen to found and lost eventsThe Listener instance used for the /subscribe/ and /unsubscribe/ methods is where your app receives the events which tell you that a device (or a message, to be precise) has been lost or found:

As you can see, no runtime checking and requesting permission is needed. It is handled for you by the following user consent dialog.One more thing that makes Nearby Messages so easy to use is the automatic Bluetooth on/off management. Keep in mind, however, that Nearby Messages can quickly drain the battery on your device, so make sure to turn it off when you’re not using it.

Playing with beacons

The example above showed how to initiate communication between two physical devices (Android phones, to be exact). That’s not all, though – Nearby Messages goes a step further and allows you to listen for messages broadcast by beacons when an application works in the background. What does it mean? Here’s a real-life story: an entertainment company launched a campaign in London to promote its recent movie. They placed 50 “Dream Jars” all around the city and built Nearby Notifications into them. As a result, if you had Bluetooth on in your mobile device and approached such a “Dream Jar”, you received a promotional notification, all thanks to the beacon magic.

If you want to start fiddling with beacons and Nearby yourself, here’s a short cheat sheet:

Now the messages will be delivered to the broadcast receiver where one can e.g. show a notification alert. Remember that it will work only if your device has Bluetooth on.

Is this secure?

As mentioned above, the messages sent with Nearby are public, like a broadcast. For this reason, they are not suitable for transmitting sensitive information – rather, they should be treated as the communication initiation stage. If you are still worried that other applications can see what you are transmitting and if your application has to handle messages from other apps, Nearby Message has you covered here. The messages are visible in the scope of the same project in the Google Developers Console. Notice the word project here: you can have multiple Android and iOS applications associated with the same project.

But wait, there’s more!

This article covered only Nearby Messages, but Nearby itself is much more than that. If you go to the official Nearby website you will see that there are also Nearby Notifications and Nearby Connection. With the former, you can receive notifications from the beacons which broadcast website URLs (it is supported natively by Android and iOS) and the latter is similar to Messages, but it’s designed to send and receive bigger payloads (audio, photos, etc.) with much greater speed in a peer-to-peer way.

What is there for you, then?

Now when you know what Nearby Messages is, when to use it and how simple it is to add it to your application, you may wonder how your business can benefit from it. The great advantage of this technology is that it reduces the problem of “how can we start talking?” to one click of a button. The market is still relatively fresh here: there aren’t that many apps of this type yet (the recent ones is the LinkedIn app), so there is space for new projects. Go ahead, then, take a closer look and build the next social media app, a local chat application or a game.