{"id":12222,"date":"2014-08-26T13:47:28","date_gmt":"2014-08-26T11:47:28","guid":{"rendered":"https:\/\/blog.trifork.com\/?p=12222"},"modified":"2014-08-26T13:47:28","modified_gmt":"2014-08-26T11:47:28","slug":"an-introduction-to-ibeacons","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/","title":{"rendered":"An introduction to iBeacons"},"content":{"rendered":"<p style=\"text-align: justify\"><img loading=\"lazy\" decoding=\"async\" style=\"float: left;margin-right: 10px\" src=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg\" alt=\"iBeacon logo\" width=\"90\" height=\"90\" \/>A little while ago I did a project where we examined the use of iBeacons in public transportation. Together with the guys from <a href=\"http:\/\/www.enigma-payments.nl\/\" target=\"_blank\" rel=\"noopener\">Enigma Consulting<\/a>, we made a demo to show what was possible and what not. In this blogpost I will show you some code examples that will hopefully get you started using iBeacons.<\/p>\n<p><!--more--><\/p>\n<p style=\"text-align: justify\">With the announcement of iOS 7 Apple also introduced us to iBeacons. iBeacons are little bluetooth devices that can be used for proximity or advertisement purposes. The beacon broadcasts a small package of data that consists of a major and a minor value. The major value is used to indicate that a specific beacon belongs to a group of iBeacons. The minor value specifies a single beacon. Using these two values a device can react to the received packages. For example: a museum puts an iBeacon next to a painting. When a visitor comes close to the painting his iPhone will react to the signal that is being transmitted by the beacon. The iPhone can look up the major and minor value in a by the developer defined set of data and shows a description of the painting.<\/p>\n<p style=\"text-align: justify\">To get started you\u2019ll first need some iBeacons. For the demo we used beacons from <a href=\"http:\/\/www.kontakt.io\">Kontakt<\/a>. They are easy to configure\u00a0and easy to use. Of course there are a lot more <a href=\"http:\/\/www.ozomedia.nl\/tip\/ibeacons-list-hardware-compatible-with-apples-ios\/\">companies<\/a> that produce iBeacons. We used four iBeacons for this project. Each beacon stood for a different action in the public transport: entering a train station, entering a train, leaving a train and checking in on a bus.<\/p>\n<p style=\"text-align: justify\">Apple has defined three kind of proximities that a beacon can have: Immediate, near and far. For entering the station we used the proximity far, for entering and leaving a train we used near and for checking in on a bus we used immediate. When the user walked to the next beacon a different image was shown to represent a different action. Below you&#8217;ll find an image of the flow of the app.<\/p>\n<p><a href=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/07\/Screen-Shot-2014-07-15-at-14.53.13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-12239\" src=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/07\/Screen-Shot-2014-07-15-at-14.53.13-212x300.png\" alt=\"flow of app\" width=\"257\" height=\"364\" \/><\/a><\/p>\n<h3>Now let\u2019s talk some code!<\/h3>\n<p style=\"text-align: justify\">First of all your class has to implement the CLLocationManagerDelegate and its functions. The iBeacons are discovered by creating a CLBeaconRegion. The CLBeaconRegion is initialised with the major value of the beacons that you want to discover. To do this I created the following function:<\/p>\n<pre class=\"brush: objc; collapse: false; title: ; wrap-lines: false; notranslate\" title=\"\">\n\n- (void)initRegion {\n    self.locationManager = &#x5B;&#x5B;CLLocationManager alloc] init];\n    self.locationManager.delegate = self;\n\n    NSUUID *uuid = &#x5B;&#x5B;NSUUID alloc] initWithUUIDString:@&quot;4A8BE066-324E-4F44-81AF-DF02D0C5B467&quot;];\n\n    self.beaconRegion = &#x5B;&#x5B;CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@&quot;nl.your.identifier&quot;];\n\n    &#x5B;self.locationManager startMonitoringForRegion:self.beaconRegion];\n\n    \/\/&#x5B;self.locationManager startRangingBeaconsInRegion:self.beaconRegion];\n}\n<\/pre>\n<p style=\"text-align: justify\">We create an UUID that is used to scan for beacons with that value as major. We then create a CLBeaconRegion and tell it to monitor for the given UUID. As you can see I commented out line 11. I used this line as a bit of a cheat. When you&#8217;re in a small environment the didEnterRegion function of the CLLocationManagerDelegate will not be triggered because you&#8217;re already in the region. To prevent this you can call startRangingBeaconsInRegion which ensures that your beacons are being found!<\/p>\n<p>To handle the behaviour of the detected beacons I used the following two functions:<\/p>\n<pre class=\"brush: objc; collapse: false; title: ; wrap-lines: false; notranslate\" title=\"\">\n\n-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {\n    for(CLBeacon *beacon in beacons){\n        &#x5B;self actionForBeacon:beacon];\n    }\n}\n<\/pre>\n<p>The function actionForBeacon contains all the logic to handle what output is shown to the user. That function looked something like this:<\/p>\n<pre class=\"brush: objc; collapse: false; title: ; wrap-lines: false; notranslate\" title=\"\">\n-(void) actionForBeacon:(CLBeacon *)beacon{\n    UIImage *image;\n    if (&#x5B;beacon.minor isEqualToNumber:&#x5B;NSNumber numberWithInt:1]]) {\n        if(beacon.proximity &lt;= CLProximityFar){\n            image = &#x5B;UIImage imageNamed:@&quot;image1&quot;];\n            &#x5B;self.imageView setImage:image];\n        }\n    }\n}\n<\/pre>\n<p style=\"text-align: justify\">This function checks if the beacon is the one we&#8217;re looking for. If it is also in the right range a new image will be shown. A similar piece of code is used for every beacon that needs to be detected.<\/p>\n<p>&nbsp;<\/p>\n<h3>Lessons learned<\/h3>\n<p style=\"text-align: justify\">One of the problems we&#8217;ve encountered is that it&#8217;s very hard to determine proximity with just one beacon\u00a0per location. The proximity was constantly fluctuating, sometimes a beacon was detected to soon and other times it was detected to late. This can be prevented \u00a0by using multiple iBeacons. In an ideal set-up you should have three beacons per location so you can use <a href=\"http:\/\/en.wikipedia.org\/wiki\/Triangulation\">triangulation<\/a>.<\/p>\n<p style=\"text-align: justify\">Another problem was that the demo could only be shown when the beacons were nearby. To cover this we build\u00a0an &#8220;offline&#8221; functionality in the app so the users could swipe their way through the demo instead of walking.<\/p>\n<p style=\"text-align: justify\">I had a fun time discovering iBeacons and the possibilities are endless. If you want to know more about iBeacons there is a <a href=\"http:\/\/gotocon.com\/amsterdam-2014\/goto-academy\/iBeacon-training\">training<\/a> coming up by\u00a0Adrian Kosmaczewski. Also, a free\u00a0<a href=\"https:\/\/secure.trifork.com\/amsterdam-2014\/freeevent\/index.jsp?eventOID=6478\">GOTO\u00a0night<\/a> will be held in Amsterdam in September.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little while ago I did a project where we examined the use of iBeacons in public transportation. Together with the guys from Enigma Consulting, we made a demo to show what was possible and what not. In this blogpost I will show you some code examples that will hopefully get you started using iBeacons.<\/p>\n","protected":false},"author":98,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[354,10],"tags":[],"class_list":["post-12222","post","type-post","status-publish","format-standard","hentry","category-objective-c","category-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>An introduction to iBeacons - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An introduction to iBeacons - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"A little while ago I did a project where we examined the use of iBeacons in public transportation. Together with the guys from Enigma Consulting, we made a demo to show what was possible and what not. In this blogpost I will show you some code examples that will hopefully get you started using iBeacons.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-26T11:47:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg\" \/>\n<meta name=\"author\" content=\"Rick Slot\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Slot\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/\",\"url\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/\",\"name\":\"An introduction to iBeacons - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg\",\"datePublished\":\"2014-08-26T11:47:28+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/7a38bf82342161156e287c136ac56b77\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#primaryimage\",\"url\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg\",\"contentUrl\":\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"An introduction to iBeacons\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/7a38bf82342161156e287c136ac56b77\",\"name\":\"Rick Slot\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/95a43e7e75d1e5d4f3b970b27c3c92c3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/95a43e7e75d1e5d4f3b970b27c3c92c3?s=96&d=mm&r=g\",\"caption\":\"Rick Slot\"},\"sameAs\":[\"http:\/\/www.trifork.nl\"],\"url\":\"https:\/\/trifork.nl\/blog\/author\/ricks\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An introduction to iBeacons - Trifork Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/","og_locale":"en_US","og_type":"article","og_title":"An introduction to iBeacons - Trifork Blog","og_description":"A little while ago I did a project where we examined the use of iBeacons in public transportation. Together with the guys from Enigma Consulting, we made a demo to show what was possible and what not. In this blogpost I will show you some code examples that will hopefully get you started using iBeacons.","og_url":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/","og_site_name":"Trifork Blog","article_published_time":"2014-08-26T11:47:28+00:00","og_image":[{"url":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg","type":"","width":"","height":""}],"author":"Rick Slot","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Slot","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/","url":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/","name":"An introduction to iBeacons - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#primaryimage"},"image":{"@id":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#primaryimage"},"thumbnailUrl":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg","datePublished":"2014-08-26T11:47:28+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/7a38bf82342161156e287c136ac56b77"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#primaryimage","url":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg","contentUrl":"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2014\/08\/iBeacon-logo.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/an-introduction-to-ibeacons\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"An introduction to iBeacons"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/7a38bf82342161156e287c136ac56b77","name":"Rick Slot","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/95a43e7e75d1e5d4f3b970b27c3c92c3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/95a43e7e75d1e5d4f3b970b27c3c92c3?s=96&d=mm&r=g","caption":"Rick Slot"},"sameAs":["http:\/\/www.trifork.nl"],"url":"https:\/\/trifork.nl\/blog\/author\/ricks\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/12222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/98"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=12222"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/12222\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=12222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=12222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=12222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}