{"id":9516,"date":"2014-02-10T09:00:04","date_gmt":"2014-02-10T14:00:04","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=9516"},"modified":"2014-02-10T09:00:04","modified_gmt":"2014-02-10T14:00:04","slug":"use-ibeacons-in-android-with-c","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/use-ibeacons-in-android-with-c\/","title":{"rendered":"Use iBeacons in Android with C#"},"content":{"rendered":"<p>\t\t\t\tSince Apple introduced iBeacons in iOS 7, the interest in them has skyrocketed. For example, Apple is <a href=\"http:\/\/mobile.theverge.com\/2013\/12\/6\/5181302\/apple-store-ibeacon-rollout\" title=\"Apple Store iBeacon Rollout\">using them in their retail stores<\/a>, the NFL <a href=\"http:\/\/www.businessinsider.com\/nfl-ibeacons-in-new-york-for-super-bowl-2014-2\" title=\"NFL iBeacons at Super Bowl\">installed them at the Super Bowl<\/a> and Major League Baseball will be <a href=\"http:\/\/www.theverge.com\/2014\/1\/31\/5363834\/mlb-apple-ibeacons-to-20-teams-ballparks-in-march\" title=\"MLB iBeacons\">using them during the upcoming season<\/a>. We have discussed previously how to implement iBeacons in your <a href=\"http:\/\/www.xamarin.com\/ios\" title=\"Create iOS apps in C# and Visual Studio with Xamarin\" target=\"_blank\">Xamarin.iOS<\/a> apps with a <a href=\"\/play-find-the-monkey-with-ios-7-ibeacons\/\" title=\"Play Find the Monkey with iBeacons\" target=\"_blank\">&#8216;Find the Monkey&#8217;<\/a> game and how to leverage them in a <a href=\"\/apple-stores-use-ibeacons-and-so-can-you\/\" title=\"Apple Stores use iBeacons and so can you\" target=\"_blank\">retail store environment<\/a>, but today we will be discussing how to bring the same iBeacon support to your <a href=\"http:\/\/www.xamarin.com\/android\" title=\"Create Android apps in C# and Visual Studio with Xamarin\" target=\"_blank\">Xamarin.Android<\/a> apps.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/xplat_findmonkey.png\" alt=\"xplat_findmonkey\" width=\"437\" height=\"428\" class=\"aligncenter size-full wp-image-9535\" \/><\/p>\n<p>iBeacons allow devices to discover their proximity to &#8220;beacons&#8221;, which can be dedicated third-party hardware or iOS devices. Previously, discovering iBeacons was limited to iOS 7. However, thanks to the work of <a href=\"https:\/\/github.com\/RadiusNetworks\/android-ibeacon-service\" title=\"Radius Networks iBeacon service\">Radius Networks<\/a> and the <a href=\"http:\/\/components.xamarin.com\/view\/xamarin-android-ibeacon-service\" title=\"Android iBeacon component\">Xamarin.Android iBeacon Service Component<\/a> created by <a href=\"https:\/\/twitter.com\/chrisriesgo\" title=\"Chris on Twitter\" target=\"_blank\">Chris Riesgo<\/a>, you can now discover iBeacons from Android devices that support Bluetooth LE.<\/p>\n<p>Let&#8217;s look at how to use the <strong>Android iBeacon Service component<\/strong> to discover an iBeacon, which we&#8217;ll run inside an iOS application using Core Bluetooth to advertise the iBeacon as shown below:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar uuid = new NSUuid (&quot;A1F30FF0-0A9F-4DE0-90DA-95F88164942E&quot;);\nvar beaconId = &quot;iOSBeacon&quot;;\nvar beaconRegion = new CLBeaconRegion (uuid, beaconId) {\n  NotifyEntryStateOnDisplay = true,\n  NotifyOnEntry = true,\n  NotifyOnExit = true\n};\n\nvar peripheralData = beaconRegion.GetPeripheralData (new NSNumber (-59));\n\nperipheralDelegate = new BTPeripheralDelegate ();\nperipheralMgr = new CBPeripheralManager (peripheralDelegate, DispatchQueue.DefaultGlobalQueue);\nperipheralMgr.StartAdvertising (peripheralData);\n<\/pre>\n<p>Once the iBeacon is advertising, other iOS 7 apps and Android apps can discover it.<\/p>\n<p>Chris&#8217;s component, like all Xamarin components, includes a sample project. In this case, the sample project is an Android application that works with the <a href=\"https:\/\/github.com\/mikebluestein\/FindTheMonkey\" title=\"iOS Find the Monkey sample\">iOS sample from the &#8220;Find the Monkey&#8221; post<\/a>. This code makes for a great starting point to build your own application that uses iBeacons. Included in the example is the source for a couple classes called <strong>MonitorNotifier<\/strong> and <strong>RangeNotifier<\/strong>, which we can use in our application to handle beacon region monitoring and ranging respectively.<\/p>\n<pre class=\"lang:csharp decode:true\">\nmonitorNotifier = new MonitorNotifier ();\nmonitoringRegion = new Region (BEACON_ID, UUID, null, null);\n\nrangeNotifier = new RangeNotifier ();\nrangingRegion = new Region (BEACON_ID, UUID, null, null);\n<\/pre>\n<p>With these notifier classes in place, we can create a <em>beacon manager<\/em>, which will coordinate all the beacon discovery and ranging updates.<\/p>\n<pre class=\"lang:csharp decode:true\">\nbeaconMgr = IBeaconManager.GetInstanceForApplication (this);\n<\/pre>\n<p>The beacon manager is handed the instances of the monitor and range notifiers, after which it can start monitoring and ranging.<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic void OnIBeaconServiceConnect ()\n{\n  beaconMgr.SetMonitorNotifier (monitorNotifier);\n  beaconMgr.SetRangeNotifier (rangeNotifier);\n\n  beaconMgr.StartMonitoringBeaconsInRegion (monitoringRegion);\n  beaconMgr.StartRangingBeaconsInRegion (rangingRegion);\n}\n<\/pre>\n<p>To implement code to handle region monitoring and ranging, we simply bind the <a href=\"http:\/\/docs.xamarin.com\/guides\/android\/application_fundamentals\/activity_lifecycle\/\" title=\"Activity lifecycle documentation\" target=\"_blank\">Activity<\/a> to the beacon manager and wire up the appropriate event handlers:<\/p>\n<pre class=\"lang:csharp decode:true\">\nbeaconMgr.Bind (this);\n\nmonitorNotifier.EnterRegionComplete += EnteredRegion;\nmonitorNotifier.ExitRegionComplete += ExitedRegion;\n\nrangeNotifier.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegion;\n<\/pre>\n<p>In this app we&#8217;ll present messages to the user and show an image with a QR code when the Android device discovers the beacon in range:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvoid EnteredRegion (object sender, MonitorEventArgs e)\n{\n  ShowMessage (&quot;Welcome back!&quot;);\n}\n\nvoid ExitedRegion (object sender, MonitorEventArgs e)\n{\n  ShowMessage (&quot;Thanks for shopping here!&quot;);\n}\n\nvoid RangingBeaconsInRegion (object sender, RangeEventArgs e)\n{\n  if (e.Beacons.Count &gt; 0) {\n    var beacon = e.Beacons.FirstOrDefault ();\n    \n    switch ((ProximityType)beacon.Proximity) {\n      case ProximityType.Immediate:\n      case ProximityType.Near:\n      case ProximityType.Far:\n        ShowMessage (&quot;Here's a coupon!&quot;, true);\n        break;\n      case ProximityType.Unknown:\n        ShowMessage (&quot;Beacon proximity unknown&quot;);\n        break;\n      }\n    }\n}\n<\/pre>\n<p>When we get near the beacon, we see the message change and the image with the QR code is displayed.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/android_beacon.png\" alt=\"android_beacon\" width=\"360\" height=\"640\" class=\"aligncenter size-full wp-image-9527\" \/><\/p>\n<p>As you can see looking at this and our previous posts the code across scenarios is very similar, at least for the iBeacon part. Thanks to this component you can do it all in C# on both iOS and Android.<\/p>\n<p>The source from this post is available in my <a href=\"https:\/\/github.com\/mikebluestein\/AndroidBeaconDemo\" title=\"AndroidBeaconDemo source\">GitHub repo<\/a>.<\/p>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/13151\/\" title=\"Discuss in forums\"><em>Discuss this post in the Xamarin forums.<\/em><\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Apple introduced iBeacons in iOS 7, the interest in them has skyrocketed. For example, Apple is using them in their retail stores, the NFL installed them at the Super Bowl and Major League Baseball will be using them during the upcoming season. We have discussed previously how to implement iBeacons in your Xamarin.iOS apps [&hellip;]<\/p>\n","protected":false},"author":1932,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-9516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Since Apple introduced iBeacons in iOS 7, the interest in them has skyrocketed. For example, Apple is using them in their retail stores, the NFL installed them at the Super Bowl and Major League Baseball will be using them during the upcoming season. We have discussed previously how to implement iBeacons in your Xamarin.iOS apps [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/9516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1932"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=9516"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/9516\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=9516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=9516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=9516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}