Using the AltBeacon library by Radius Networks we can easily add iBeacon monitoring and ranging support to our native Android application. We’re going to see how to scan for a variety of proximity beacons and display them within an application.

In our example we will be scanning for beacons and adding them to a list in the Android UI. I am personally using a variety of iBeacons to test with including those from Gimbal, Estimote, and Radius Networks.

Let’s start by creating a fresh Android project. You can do this with Android Studio, or you can do this from the Terminal (Mac and Linux) or Command Prompt (Windows):

I’m personally using Android Studio, but both solutions should work without issue. For more information on using the command line, see a previous article I wrote on the topic.

The first thing we want to do is include the AltBeacon library in our Gradle build process. Find the project’s build.gradle file and add the following line to the dependencies section:

compile 'org.altbeacon:android-beacon-library:2+'

Now we’ll be able to use the library in our project.

The AltBeacon library requires certain application permissions be set. For example, iBeacons are Bluetooth devices so we must enable various Bluetooth permissions. Since they work off proximity, we’ll also need various location services enabled. All of this can be done in the app/src/main/AndroidManifest.xml file. Open this file and include the following lines:

These permissions should be added before the application tag, but after the manifest tag.

The beacons that we discover from this application will be placed into a list. This means we need to alter the layout to our activity to include an Android ListView component. Open the project’s app/src/main/res/layout/activity_main.xml and include the following XML markup:

Much of the above is pretty standard Android initial layout code. We just included a ListView component in it.

Now we can focus on all the code involved in scanning for iBeacons and populating the list that we just created in the UI. Open the project’s app/src/main/java/com/nraboy/beaconproject/MainActivity.java and include the following code:

In the above code we set up a parser that will look for Bluetooth packets that match the layout. Not all Bluetooth devices are iBeacons, so we are specifying to only scan for hardware that meets the specification.

Let’s skip ahead to the onBeaconServiceConnect method. Here we’ll define a listener for beacons that are in range and define which beacons to listen for.

For the above to make sense, let’s figure out what beacons we’re going to listen for. You see that we are defining a single region with only a region id. The beacon id, major, and minor codes are left as null. This means we want all beacons that match the iBeacon specification to be listened for.

Jumping back to the didRangeBeaconsInRegion method, we can receive any number of iBeacons in a single notification. We choose to iterate over each beacon returned and add them to the list.

So far so good right?

Well, starting in Android 5.0, users need to grant permission to use various device features within the application. If permission is not requested, the functionality will not work.

The above code is how we request permission for certain device features. Of course we don’t really need to show an alert, but it is a good idea to explain why you are going to ask for permission. Once permission is granted, Bluetooth and location services can be used.

Now in most scenarios we won’t want to scan for any and every iBeacon that exists. We probably want to set specific iBeacons based on maybe a remote database. Let’s modify the onBeaconServiceConnect method a bit:

I’ve added two beacon regions specific to two of my iBeacons. Although we are scanning for all iBeacons, we are only listening for those two regions now. The didRangeBeaconsInRegion method changed a bit too. Although we can potential listen for multiple iBeacons per region, in my case I only have one per each. Instead of refreshing the list every time, we are just adding them if they don’t already exist.

The application should be runnable at this point.

Conclusion

We just saw how to use the AltBeacon library in our native Android application to scan for iBeacons and set up regions. There are plenty of other features as part of the AltBeacon library, such as monitoring, but that is best left for your imagination.

The iBeacons I used to test this code were from Gimbal, Estimote, and Radius Networks, but any Bluetooth device that follows the iBeacon specification should work.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.