altbeaconBeacons. These small devices might become ubiquitous in the future. It started with iBeacons from Apple, AltBeacon, and Google is now getting it’s own platform called Eddystone. This is going to be interesting.

If you don’t know what a beacon is, it’s a dumb thing that only knows how to emit its presence to the world. That is the sole purpose of a beacon. Telling to whoever is listening in range that it’s here and it’s name is e9ae6b9f-7238-4df6-8497-dec29ebf4184 (or something like that). It does that using Bluetooth LE (Or Bluetooth Low Energy, BLE, Bluetooth Smart).

This means that a beacon is useful when you have an application that know how to react to the beacon’s presence. The app needs to know what to do when the user encounters the beacon. And maybe the thing to do will change in time. If you walk across a beacon in a supermarket, the app might show you what is currently on sales. And obviously this will change in time. So a good thing when working with beacons is to be able to repurpose them. This is something that Couchbase Mobile can offer you. Easily sync back a new behavior for a particular beacon.

And of course since it works offline, it can always do the intended thing even if the user has no network connection. You can also save a document locally with information about the user being in range of the beacon and sync it back later to the server. It can give you some interesting data about your users behavior in a place without or with poor network connectivity. Think about any big outdoor event for instance.

I started toying around with these ideas for various conferences and meetups, here’s how to get started with Beacons and Android.

Testing Beacons

Being able to test beacons is the first thing I wanted to do. And the reason to that was, I have no beacons :). Which my sound weird I know. But a beacon is just a low energy bluetooth device. I have bluetooth on my mac, I also have some BLE dongles and a RaspberryPi, everything should be fine.

The wonderful people at Radius Networks built a VM image that can very simply act as a beacon. Everything is explained on their website. If you have a Mac and want to use its Bluetooth hardware in VirtualBox, know that you have to disable it first for the VM host. If you have bluetooth dongle than it’s really quick, just make sure VirtualBox is configured to use it.

They also built PiBeacon. It’s pretty much a Beacon development kit based on the RaspberryPI. And you can download the image for the Raspberry here. Complete instructions can also be found on their website.

Once you have the Raspberry running with their image, you can SSH into it and run something like

This will simulate a beacon transmitter. They have three identifiers. A UUID, a major version and a minor version. This is part of the common layer between every Beacons format like AltBeacon(Open format), iBeacon(Apple) and EddyStone(Google). And this is what apps use to identify beacons.

I wanted to make sure everything was working correctly so I installed BeaconScanner on my Mac and another Beacon scanning app on my phone. They both picked up the signal so I was ready to work on my app.

 Android and Beacons

I wanted to keep everything as simple as possible so I went for the AltBeacon stack. Everything is open source and they have a nice Android library. They also have nice, easy to undertand code samples that made it easy to start. So first things first, include the right dependency to your app. Start by adding the JCenter maven repository:

Then add the actual library:

Something else you can modify right of is adding the right permission to your application AndroidManifest.xml:

There are basically two things you can do with Couchbase Mobile. You can act as a client and react to a beacon presence, or you can act as a backend and configure the beacons.

Let’s start by the code that initialize the beacon background service. In your Application, upon create, you can execute the following code:

I have setup some aggressive timing for testing purposing. You would use longer duration in production. My Application class also implements the BootstrapNotifier interface. The didEnterRegion method allows a specific activity to be launched when coming in range of a beacon.

This activity will contain the code you want to show depending on the beacon ID for instance. Or maybe you just want to save a document to log that the current user has been in range of a particular beacon. Which is the first thing I want to do. So I need an activity implementing the BeaconConsumer interface.

This is some very basic code but you get the idea. At this time you could also sync a channel named after the identifier of the Beacon. This way you get all the information you need to display meaningful information to the user. And if this information change over time, all you have to do is modify the document containing the beacon behavior and sync it back to all users. It’s really easy to build a beacon backend with the Sync Gateway REST API :)

So what do you think about Beacons? Are you a user already? Would you like us to talk more about it? Please let us know what you think!

Author

Posted by Laurent Doguin, Developer Advocate, Couchbase

Laurent is a Paris based Developer Advocate where he focuses on helping Java developers and the French community. He writes code in Java and blog posts in Markdown. Prior to joining Couchbase he was Nuxeo’s community liaison where he devoted his time and expertise to helping the entire Nuxeo Community become more active and efficient.

Leave a reply