Mike Bluestein December 6, 2013

Today, Apple began using iBeacons in all of their retail stores across the United States. In this article, I’ll show you how C# developers can also take advantage of this exciting technology.

We’re going to take a look at iBeacons and Multipeer Connectivity, and show how they can be used in concert to create a unique in-store experience for customers with a coupon-serving iOS app.


For this app, when a customer enters the store, the iPhone version of the application will find an iBeacon in the store, and enable the customer to get a coupon for that store. In the store itself, an iPad will serve as the iBeacon, as well as handle sending the coupon information to the customer.

To advertise itself as an iBeacon, the app will:

  1. Create a beacon region.
  2. Get peripheral data from the beacon region.
  3. Create a peripheral manager.
  4. Use the peripheral manager to advertise the peripheral data.

Creating a beacon region is done by passing an NSUuid to a CLBeaconRegion instance:

Then, we can use the CLBeaconRegion to get an NSMutableDictionary containing the peripheral data for a specified beacon power:

With the CLBeaconRegion and peripheral data in place, we can create a CBPeripheralManager to advertise the beacon:

That’s all you need to do to advertise an iBeacon. Now let’s see how we can discover it from the iPhone app.

To discover when we enter the beacon region, we use the Core Location framework. In Xamarin.iOS we can take advantage of location manager delegate methods being wrapped in C# events. We need to handle the RegionEntered event for when the region is entered, in addition to the DidDetermineState event, for the case where the beacon is first discovered when already in the beacon region (such as the case where the iPad is turned on with the customer carrying the iPhone already being in the store). Note, we set NotifyEntryStateOnDisplay above to get this event in all cases.

The WelcomeBackCustomer method simply presents a notification and enables a button to allow the user to connect to the iPad to get a coupon.

In this case, we use the iPad to browse for peers (customers) using Multipeer Connectivity framework’s MCBrowserViewController, as shown below:

On the iPhone, the app uses an MCAdvertiserAssistant to advertise itself to peers:

Then, in the MCSessionDelegate, we send a coupon when the customer has connected:

Finally SendCoupon uses the MCSession to send the coupon information in a string for simplicity.

The coupon image is generated on the receiver using the CIQRCodeGenerator filter from Core Image:

The code for this post is available in my github repo.