Building iBeacon applications on Android

This post is part of the series on working with beacons and mobile devices and was written by our CTO, Arvid E. Picciani (aep). Arvid is an ex-Nokia engineer, IoT pioneer, and self-proclaimed embedded devices hacker.

Apple’s iBeacons are really Bluetooth in the core. Bluetooth Low Energy came out of Nokia, an important innovator in mobile wireless technology. It was renamed from Wibree to Bluetooth 4.0 when it was handed over to the Bluetooth Special Interest Group (SIG). It was designed from the ground up for super low battery consumption and succeeded. The major competitor Near Field Communication (NFC) uses even less power (zero), at a shorter range (centimeters), but is not available in popular devices like the iPhone.

On top of Bluetooth Low Energy, there are several alternative approaches to build proximity applications, such as Qualcomm’s Gimbal or Samsung’s Flybell. Each of them adds features or implements them differently. We currently assert that they will not receive the benefits of some iOS features, such as waking an app up near a beacon. In this post, i’d like to explain Apple’s configuration a little bit in details, which is a pre-requirement to understanding how to use them on Android.

Android brings Bluetooth Low Energy support with version 4.3. Unlike iOS, Android does not treat beacons in any special way and does not offer any core libraries. As an application developer, rather than fighting the OS API, you have to be aware of how beacons actually work, and implement most of the framework yourself.

iBeacons emit Bluetooth non-connectable announcement frames at a frequency of about 100-400ms with static content. You would fetch the first type of frame and parse its Manufacturing Specific Data field.

To get started with beacons on Android, you’d need an Android phone that supports Bluetooth 4.0 (Nexus 4, 5) and the Android developer environment ready setup. If you do not have a hardware beacon available, an iPhone will do as well. Download the “iBeacon Locate” app from the App Store and enable it to broadcast a beacon. If you do not have an iPhone, a Linux laptop with a Bluetooth 4.0 internal or USB stick will work as well. Basically just run:

sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 BE EF BE EF  BE EF BE EF BE EF BE EF DE AD F0 0D 00 00 00 00 C5 00

Getting started with your app is either really hard (implement your own Bluetooth scanner daemon and so on) or really easy thanks to the friendly folks at RadiusNetworks who give us a library that already does that for us.
You can get it at their GitHub

It implements most of the hard parts and makes it available in a similar fashion to the iOS API, which I explained in the previous series.

Start with binding the service:

private IBeaconManager iBeaconManager =   IBeaconManager.getInstanceForApplication(this);

protected void onCreate(Bundle savedInstanceState) {
    // ...
    iBeaconManager.bind(this);
}

…and then start ranging beacons with a RangeNotifier

public void onIBeaconServiceConnect() {
  iBeaconManager.setRangeNotifier(new RangeNotifier() {
     @Override 
     public void didRangeBeaconsInRegion(Collection iBeacons, Region region) {
          Log.i(TAG, "I see Bacon "+iBeacons.iterator().next().getAccuracy()+
          " meters away!”);               
      }
   });

   try {
     iBeaconManager.startRangingBeaconsInRegion(new 
        Region("beefbeef-beef-beef-beef-beefdeadf00d" , null, null, null));
     } catch (RemoteException e) {   }
}

Android has its own set of quirk, that the library works around for you. But you should keep in mind that bound Android services may get killed when your UI is no longer visible, so build your application accordingly.

The same physical restrictions as on iPhones, also apply on Android. Bluetooth Low Energy and WiFi share the same frequency, and sometimes the same antenna. Unlike on iOS, where WiFi always takes priority and sometimes disables Bluetooth scanning, on Android it is the opposite. When continuously scanning for beacon frames, the users internet connection may be impaired.

 
68
Kudos
 
68
Kudos

Now read this

What Bluetooth Low Energy means for the IoT maker community

Connecting your microcontrollers to the internet There are alot of wireless standards aiming at lower-than-wifi power, zigbee, zwave, enotion to name a bunch, but none of them have wide consumers application, making it difficult for us... Continue →