Internet of Things – iBeacons

Today I will talk about how you can use iBeacons with mobile apps. I will create a simple iPhone app that can find an iBeacon, and show information about it.

The idea of iBeacons is quite simple: it’s a small device that sends out a signal that a mobile phone (or tablet) can pick up and act on. The technology used to send the signal is a version of Bluetooth using low energy, so that its battery can last longer. Depending on the signal’s strength, the phone can determine the distance to the iBeacon, and on iOS, the distinctions are made between immediate (within a few centimeters), near (within a couple of meters), and far (greater than 10 meters, and up to about 50 meters). The information in the signal is mainly an identity, which is composed by a UUID, and a major and minor numeric values. When an app in the phone receive the signal, the normal scenario is that it will use the identity to request information from a service (often in the cloud), and show the result of the response. In this example the iBeacon is placed close to a specific shoe, and therefore the service will return information about the shoe, which is then shown in the app.

Let’s create an iPhone app that listens to iBeacons, and to do that you first need an iPhone 4S or later. You also need to be a registered iOS developer (you can enroll at https://developer.apple.com/programs/ios), and then you have to download and install the development tools (Xcode with SDK). Finally, you need an iBeacon, and I’m using one from Estimote (www.estimote.com), but there are many others to choose from.

Now it’s time to open Xcode and create a new project as a single view iOS application, and start by adding the framework CoreLocation. Then update AppDelegate.h to look like shown here…

…and first we import CoreLocation (on line 1) as well as add the location manager delegate (on line 2). Then we add two properties for the beacon region and the location manager (on lines 3-4).

Now update the implementation part of AppDelegate.m to look like shown here…

…and first we set up the location manager (on lines 3-4). Then the beacon region is set up (on lines 5-7, and please replace the zeros with the UUID of your iBeacon), and the location manager is instructed to start looking for the region (on line 8). In the didDetermineState method, we start by checking if we just entered or left the region (on line 13 and 27), and if we entered the region, we start looking for iBeacons (on line 15), and check whether the app is in the background (on line 18). If it is, a local notification is shown welcoming the user to the store (on lines 20-22), and if the app is in the foreground (active), an alert is shown with the same message instead (on line 25). If we left the region, we just stop looking for iBeacons (on line 28). When one or more iBeacons are found in didRangeBeacons, we show information about each one of them (on lines 32-34).

The idea is that we have a chain of stores where the iBeacons in each store have the same UUID. Inside the store, the different iBeacons have different major (e.g. for department) and minor (e.g. for individual location) values. As we are just looking for the UUID when showing the welcome message (in didDetermineState), it can be triggered by any of the iBeacons in the store. Please note that this also works if the app is not in the background (i.e. not started), and even if the phone is sleeping (i.e. the local notification will show up on the lock screen), as iOS will keep track of which apps are looking for which iBeacons and wake them up when any of the right iBeacons are close. Then, when we get the information for each iBeacon (in didRangeBeacons), we can send a request to the server to ask for more information (e.g. that this beacon is connected to a special offer), and then show it to the user (after asking the user for a confirmation). Even if the concept of iBeacons is simple, it’s very powerful, and that’s why we will see these things everywhere in the coming years.

That’s how you can use iBeacons in your apps.