Xamarin Code for iBeacons

Did I mention how easy it is to track an iBeacon using Xamarin?

[code lang=”csharp”]
locationManager = new CLLocationManager();
var beaconId = new NSUuid("E437C1AF-36CE-4BBC-BBE2-6CE802977C46");
var beaconRegion = new CLBeaconRegion(beaconId, "My Beacon");
locationManager.RegionEntered += (s, e) => {
if(e.Region.Identifier == "My Beacon")
{
Console.WriteLine("Found My Beacon");
//Fire up ranging
locationManager.StartRangingBeacons(beaconRegion);
locationManager.DidRangeBeacons += (lm, rangeEvents) => {
switch(rangeEvents.Beacons[0].Proximity)
{
case CLProximity.Far:
Console.WriteLine("You’re getting colder!");
break;
case CLProximity.Near:
Console.WriteLine("You’re getting warmer!");
break;
case CLProximity.Immediate:
Console.WriteLine("You’re red hot!");
break;
case CLProximity.Unknown:
Console.WriteLine("I can’t tell");
break;
default:
throw new ArgumentOutOfRangeException();
}
};
}
};
locationManager.StartMonitoring(beaconRegion);
//Create a beacon
var peripheralManager = new CBPeripheralManager(new MyPeripheralDelegate(), DispatchQueue.DefaultGlobalQueue, new NSDictionary());
var beaconOptions = beaconRegion.GetPeripheralData(null);
peripheralManager.StartAdvertising(beaconOptions);
[/code]

8 thoughts on “Xamarin Code for iBeacons

Comments are closed.