Xamarin Forms + AltBeacon – technology spike test

Michał is the Lead Developer for our team based in Warsaw.

Once a month in Mountain Warehouse we have a self-development day. It is a chance for our team to improve programming skills, attend tech debates, watch inspiring tutorials or start a small new project using latest technologies. This time my team decided to jump into a small mobile project which could be useful for our retail business.

A mobile application for customers, which is activated and attracts customer to enter the MountainWarehouse shop while passing by, seems to be the best solution to meet both retail business and programmers expectations.

As a .net oriented team, the decision to choose a mobile technology was straightforward – Xamarin (https://www.xamarin.com). Xamarin is an ideal technology for those who want to start developing mobile apps without learning Java/Kotlin/Objective-C. With Xamarin you can deliver native iOS, Android, and Windows apps using existing skills, teams, and code. You don’t even have to change your current working environment because Xamarin is supported by Visual Studio.

As for proximity beacons technology we choose open source AltBeacon technology (http://altbeacon.org/).  Proximity beacons is a piece of a hardware which emits  Bluetooth Low Energy signal. The AltBeacon specification defines the format of the advertisement message that Bluetooth Low Energy proximity beacons broadcast. We use it to broadcast information about retail shop which customer is passing by. Then the application makes a call to Azure Function Api which sends data to the client. Our initial idea was to send digital coupons to a customer, but this could be replaced by any other action performed on mobile device (a pop-up message personalized for customer, a discount, an offer or any kind of advertisement).

We decided to place our mobile application on BitBucket repo, and linked it to Visual Studio App Center (https://appcenter.ms) which is a perfect platform for setting up CI/CD pipeline. With AppCenter you can:

  • Connect to your repo and build apps automatically with every pull request or on demand
  • Automate UI tests on thousands of real-world devices and hundreds of configurations in the cloud
  • Distribute your app to beta testers and users on Android, iOS, Windows, and macOS with every commit or on demand

Our backend system was based on Azure Functions, which accelerate your development with an event-driven, server-less compute experience and provide you with scale on demand and pay only for the resources you consume.

Setting up project

Starting with empty Xamarin Forms project,  requires to set some permissions correctly. You need BLUETOOTH permission to be set. (In some scenarios you may need BLUETOOTH_ADMIN also set).

Detecting the Beacon

The best way to start is to use Xamarin Forms template. I would recommend using the sample code and building on that. It creates a solution with separate projects for iOS and Android and Common Shared Library with all Models and Services.

The application flow is as follows.

  1. Implement in MainActivity  IBeaconConsumer interface
  2. To get the instance of BeaconManager  invoke the GetInstanceForApplication method (passing your main activity as a parameter)(whole AltBeacon logic is encapsulated in AltBeaconService)
  3. Add an Beacon Parser. There is code in the sample that is marked as working for Estimote. (https://github.com/chrisriesgo/Android-AltBeacon-Library/tree/master/Samples/Android/AndroidAltBeaconLibrary.Sample).
  4. Bind to the BeaconManager. This is what starts off the process.
  5. Set up event handlers for the different notifiers.
  6. Once binding is done, you get a callback to OnBeaconServiceConnect which is part of your implementation of IBeaconConsumer
  7. Here you can  setup other notifiers you maybe interested and regions etc. If you want to listen to all beacons, just setup an Empty Region with null for UUID etc. Else you can listen only for beacons you want to listen to by using specific UUIDs.
  8. Then call StartMonitoringBeaconsInRegion to start the monitoring.
  9. Once beacons are detected then the event handlers you had mapped get triggered. The beacons in range are passed via the RangeEventArgs parameter of the event. You take the beacon data available and then do the required business action. In our scenario it is API call to Azure Functions which returns binary data.

After completing this steps, you should have a working application that can detect proximity beacon and perform an action.

For more examples look into:

  1. Android-AltBeacon-Library
  2. AltBeacon documentation
  3. AltBeacon specification
  4. Code example for Xamarin and AltBeacon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s