Nearby Messages with Beacons

Peter-John Welcome
AndroidPub
Published in
3 min readFeb 5, 2017

In my previous post about the Nearby Messages API, I mentioned that the Nearby Messages API could be used to interact with beacons. This post will focus on how that interaction works and how the Google Beacon Platform fits in.

Getting Started

To get us started we will need to get ourselves some beacons. These beacons will need to broadcast the Eddystone format. There are quite a few manufactures out there that are supporting the Eddystone format, so we are spoilt for choice.

Once we have our hardware, we will need to provision our beacon with the Google Beacon Platform.

This can be done by downloading the Beacon Tools app for Android and iOS.

Warning: Once you register a beacon on the Google Beacon Platform it will receive a Unique ID that is associated with that Beacon and will not be able to be registered again with another user or project.

Once we have the above sorted, we can add attachments to our registered beacon in the Google Beacon Dashboard. To add attachments you simply select your beacon, click on view attachments, and you will be presented with a form to fill in (as shown below). Each attachment consists of a namespace which comes from the project you have registered the beacon with in the Developer Console. There is also a Type parameter, which is a value that distinguishes each attachment within that namespace, and a value/message you would like to send from the beacon to a device.

Nearby Messages & Beacon Interaction

Now we can finally get to some code. As shown in my previous post, we can subscribe to messages that are being broadcast by other Bluetooth devices and that’s essentially what a beacon is. For us as developers though, how do we get messages to our apps from only our beacons? The Nearby Messages API provides us with a way to filter the messages that we are receiving to our app. This is done with a class called SubscribeOptions. We can build an instantiation of this class using the SubscribeOptions.Builder() method. We can set the Strategy and then the Filter as shown in the code below:

SubscribeOptions options = new SubscribeOptions.Builder()   
.setStrategy(Strategy.BLE_ONLY) .setFilter(new MessageFilter.Builder() .includeNamespacedType(getString(R.string.Namespace),
getString(R.string.Type)) .build()).build();

When setting the Filter, a MessageFilter is created where we then specify the namespace of our project and the type we set earlier in the Google Beacon Platform. We then add this SubscribeOptions object to our subscribe method when we listen for Nearby Messages:

Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options)

This will result in us only getting messages from our registered Beacon in our app.

This is really awesome :)

What’s even more awesome about this approach to interacting with beacons, is that with the Google Beacon Platform, you can now use one beacon for multiple uses. All we need to do is add multiple attachments to the beacon and specify different types. This gives us the ability to use one beacon for multiple apps and each app will receive different messages from that beacon.

This opens a world of possibilities for what we as developers can develop with beacons and increases the number of use cases we can target with just one beacon.

--

--

Peter-John Welcome
AndroidPub

Freelance Senior Mobile Engineer, Google Developer Expert for Firebase, Photographer