I few weeks back I started playing around with beacons and use of beacons in enterprise scenarios. Currently only Android and iOS devices support the advertisement mode of beacons and wanted to try out how Xamarin can be used to create cross-platform apps that can use beacons.
One of the libraries that provide a common API to work with beacons is the AltBeacon, which is an open source Beacon library. There is also a Xamarin compatible wrapper for this library at https://components.xamarin.com/view/android-altbeacon-library. Currently this is compatible only with Android (I have not yet come across an iOS and Android compatible free library), but since I was building my test app on Android, I decided to go along.
This post helped me with initial information, but the AltBeacon library has changed and so some steps have changed.
Once you install the AltBeacon library, I would recommend you first try out the sample application. I recommend that so that you can make sure you have the right dependencies and your devices are setup right etc. This helps ensure that if your code is unable to connect using he library, the problem is with the code and not the hardware…
The idea behind the post is to talk about the steps to start working with beacons using the Altbeacon. Rather than dumping code, I wanted to show the steps involved so that you can make sense of the flow and build your apps.
Setting up your project
If you start with a new project, then pls make sure you have the capabilities that are required set correctly. You need BLUETOOTH permission to be set. (In some scenarios you may need BLUETOOTH_ADMIN also set)
Then you need to setup the service entries you need in the AndroidManifest.xml. I just copied the entries from the sample project and this should work ok for you. The entries will be similar to the following:
<application android:label="BeaconDetect" android:icon="@drawable/Icon">
<service android:enabled="true" android:exported="true" android:isolatedProcess="false" android:label="Beacon" android:name="org.altbeacon.beacon.service.BeaconService"></service>
<service android:enabled="true" android:name="org.altbeacon.beacon.BeaconIntentProcessor">
<meta-data android:name="background" android:value="true" />
<action android:name="org.altbeacon.xamarinbeaconreference.MSG_STOP_RANGING" />
<action android:name="org.altbeacon.xamarinbeaconreference.MSG_STOP_MONITORING" />
<action android:name="org.altbeacon.xamarinbeaconreference.MSG_START_RANGING" />
<action android:name="org.altbeacon.xamarinbeaconreference.MSG_START_MONITORING" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
Now your project is setup.
Detecting the Beacon
I would recommend you use the sample code and build on that. But it takes quite a bit of effort to understand the flow. The application flow is as follows.
You main activity implement the interface IBeaconConsumer
Check if Bluetooth is available using the CheckAvailability() static method of the BeaconManager
Get an instance of BeaconManager using the GetInstanceForApplication Method and passing your main activity as a parameter as it is what is going to receive the Detect message (in the case of the sample)
Add an Beacon Parser. There is code in the sample that is marked as working for Estimote. I found this worked fine for Gimbal Beacons too, so we can use that as is. If you have a different type of beacon, a google search should help you get the right parser layout.
Bind to the BeaconManager. This is what starts off the process.
Setup event handlers for the different notifiers.
Once the bind is done, you get a callback to OnBeaconServiceConnect which is part of your implementation of IBeaconConsumer
Here you 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.
Then call StartMonitoringBeaconsInRegion to start the monitoring.
Once beacons are detected then the event handlers you had mapped get triggered. The beacons in range are passed via the RangeEvenArgs parameter of the event. You take the beacon data available and then do the required business action.