Indoor navigation with iBeacon in iOS7

IOS7 introduces iBeacon support. The Internet is full of headlines:
- Why iBeacon may be the most important feature of iOS7 .
- iBeacon will open a new era of location-based applications .
- How iBeacon can kill NFC .
- iBeacon and the future of mobile payments .

According to reporters, this is a super technology that will change the market for mobile payments, indoor navigation, customer habits and at the same time kill NFC.

Under the cat, I’ll tell you what iBeacon really is, how you can use it and show an example application for navigating our office.



iBeacon is a Bluetooth Low Energy beacon transmitting three proximity parameters UUID, major and minor. iOS7 makes it easy to work with such devices.

proximity UUID is a unique 128-bit identifier, all beacons of your company or within the building have the same pUUID. major and minor are 16 bit unsigned integers for numbering beacons within the same pUUID. These are Apple recommendations, you can set absolutely any values ​​for these parameters, but the recommended scheme is more convenient.

Beacon Estimote and Kontakt .


One beacon costs $ 30, works for two years on battery. Many companies are finalizing their products to support the iBeacon profile. I think that within three months there will be Chinese counterparts at $ 10.

iOS devices can act as a beacon, this allows you to play with the new API now.

In the Core Location Framework added three classes: CLBeacon, CLBeaconRegion and CLCircularRegion. The CLLocationManager introduced the methods isRangingAvailable, rangedRegions, requestStateForRegion, startMonitoringForRegion, startRangingBeaconsInRegion and the corresponding events in CLLocationManagerDelegate. startMonitoringForRegion now supports not only GPS regions but also Bluetooth. How to use it is described in detail in the documentation .

In fact, the new API provides two possibilities:
  1. Obtaining a list of beacons in the visibility zone with a certain Proximity UUID (you can narrow the search by specifying the value major or major / minor) using the startRangingBeaconsInRegion method. The API determines the distance to the beacon (Immediate, Near, Far) and the distance error in meters.
  2. Monitoring the region. When a user enters or leaves the beacon’s coverage area, the didEnterRegion / didEnterRegion events (startMonitoringForRegion method) occur. Moreover, monitoring works even when your application is in the background, when an event occurs, the application will be woken up.

You cannot use data to obtain device coordinates in a room . The explanation for the accuracy of the distance explicitly says “Do not use it to identify a precise location for the beacon”. Experiments confirm the data is not suitable for triangulation.

Possible applications of iBeacons:
  1. Indoor navigation: hang beacons in each room (the same proximity UUID, different major for floors / departments, different minor for rooms). On the map we display the beacon closest to the smartphone.
  2. Interactive tour of the museum: for each exhibit we glue lighthouses, when approaching the exhibit we show additional information about it.
  3. Accurate positioning in the city. We hang beacons on monuments and objects of architecture. The whole city turns into a museum.
  4. Navigation for the blind. When a beacon is detected, play a voice message where the user is located and where you can move on.
  5. Locational games and quests. For example, a simple demo game " find a monkey "
  6. Discounts and promotions in stores. We pass by jeans (a beacon is installed on the shelf) and we get a message about buy-two-pair-third-in-gift.
  7. Tincture of equipment. I'm not sure if this is generally connected with iBeacon, but with iOS7 Apple TV can be set up by touching the iPhone .


iPhone 4s +, iPad 3+ can work like a beacon. I decided to write a simple navigation around our office using test iOS devices. If you do not have Xamarin.iOS or the desire to build an application, try the Estimote App . The application demonstrates new features (it will require two iOS devices with Bluetooth LE).

So. Map of our office:
For navigation, you need beacons with iOS7, I typed test devices and put them in the center of cabinets 310, 311 and 312. In total, 3 beacons and one receiver were required.


As a basis, take the Xamarin AirLocate demo application . It can enable broadcasting of proximity UUID, major and minor (parameters are configurable) and display a list of beacons accessible around with given pUUIDs.

The application code for navigating our office is available on github . I just want to apologize for the terrible code, I'm a marketer and this is my first iOS application.

We set the beacons to the same proximity UUID, set major to the cabinet number. Beacon activation code (in the background it does not work, you need to turn off auto-lock):

CLBeaconRegion region = new CLBeaconRegion (uuid, major.UInt16Value, minor.UInt16Value, Defaults.Identifier);
if (region != null)
      peripheralManager.StartAdvertising (region.GetPeripheralData (power));

AirLocate provides a list of available beacons with distance and accuracy, an example from 312 offices (a beacon from 310 does not finish).


Create a MapViewController that will draw a rectangle above the cabinet, the number of which is contained in the major of the nearest beacon.
locationManager = new CLLocationManager ();
locationManager.DidRangeBeacons += HandleDidRangeBeacons;
locationManager.StartRangingBeacons (new CLBeaconRegion(Defaults.SupportedProximityUuids[0], "office_beacon"));

void HandleDidRangeBeacons (object sender, CLRegionBeaconsRangedEventArgs e)
{
       drawRegion (e.Beacons [0]);
}

switch ((int)(beacon.Major)) {
    case 310: 
        path.AddRect(roomCoords[0]);
	break;
    case 311: 
        path.AddRect(roomCoords[1]);
        break;
    case 312: 
        path.AddRect(roomCoords[2]);
       break;
}
(sorry again for the awful code).

Now you can walk around the office and test.


Works!

The accuracy of determining the cabinet is excellent, but the update speed is not very good, if you go to another office with the usual step, the map will be updated in 10-30 seconds.

The application is worth improving. Add an accuracy threshold so that in neighboring rooms and a corridor without lighthouses, the map does not draw cabinets with beacons. And draw not rectangles, but circles with accuracy radius. But enough for demonstration purposes.

On the whole, iBeacon is nothing revolutionary, but Apple definitely gave a strong impetus to the development of location-based applications. For iOS7, they are written in a dozen lines of code.


A picture in the header from estimote.com, images of lighthouses from manufacturers' websites.

Subscribe to our habr-blog . Every Thursday, useful articles on mobile development, marketing and the mobile studio business.