In 2013 Apple Inc. introduced a technology called iBeacon that runs on top of Bluetooth 4.0 protocol which is also known as Bluetooth Low Energy or BLE.  While Bluetooth enabled devices can identify themselves, the process is generally 1 to 1 with a host pairing with a device, such as a smart phone pairing with a Bluetooth headset.  The iBeacon is simply a broadcast service that sends out a few pre-configured pieces of information and can run in parallel with other Bluetooth services.  For discussion, the four parameters of interest when creating beacon applications from solutions like Estimote or from Gimbal are the UUID String, a major 16-bit number, a minor 16-bit number, and a signal strength value.

On iOS7, Apple Inc. provides library functionalities for working with iBeacons and while there are 3rd party libraries for other platforms, the blog here will be focused on iOS.  Combining Couchbase Mobile technologies, Ed Arenberg and the EPage team is developing a service that makes heavy use of iBeacon and utilizes Couchbase Lite for local storage while synchronizing data among numberous devices by using Couchbase Sync Gateway.

Let us now explore the 3 key components of this iBeacon service and how to implement these core features using Couchbase Mobile technologies. Note all of the code is written in Swift 1.2.

Saving data locally on the device to Couchbase Lite

The service will first collect information from nearby beacons and log device informations to the database.  For your exploration, Apple has a good amount of documentation on how to locate and range nearby iBeacons.  For integrating Couchbase Lite into your mobile app, you may look at the Couchbase Mobile developer portal for reference sample code. 

ORM Implementation

We will have the app keep objects in native classes and will implement an ORM to map to and from the Couchbase representation.  When the app locates a group of iBeacons, their class objects will be generated.  The class includes code for saving the object to Couchbase and for loading it from Couchbase.  Here is the class for holding an iBeacon:

 

Syncing Beacon Data

When you are connected to a network, Couchbase Mobile will sync out data using Couchbase Sync Gateway.  This is handled automatically once we set up our push and pull replications, which makes it very easy to keep the system coordinated.  We can add an observer method to listen for the state of the push and pull operations.  However, each device needs to listen for changes in the database for which it needs to take action, so we can add an observer for a database change notification.  When the database changes, we update our local native objects.  To manage the Couchbase interaction, create a Database object that instantiates a singleton.  The main concepts are delineated in the code below:

These are the main pieces that allow the mobile app to run local activities such as monitoring for nearby beacons, saving its local state, and set up sync services to coordinate information consistency amongst app instances.  From this you can build up more functionality and create sophisticated services that can synchronize state across a network of apps. In the next blog we will explore the use of beacon and Couchbase Mobile technologies together to deliver location aware apps that work without a network connection.

Author

Posted by William Hoang, Mobile Developer Advocate, Couchbase

William was a Developer Advocate on the Mobile Engineering/Developer Experience team at Couchbase. His love for coffee and code has transcended him into the world of mobile while appreciating the offline in-person experiences. Prior, William worked on the Developer Relations team over at Twitter, BlackBerry, and Microsoft while also having been a Software Embedded GPS engineer at Research In Motion. William graduated from McGill University in Electrical Software Engineering

Leave a reply