{"id":25348,"date":"2015-08-10T00:05:39","date_gmt":"2015-08-09T18:35:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=25348"},"modified":"2024-01-02T17:48:48","modified_gmt":"2024-01-02T12:18:48","slug":"beacon-implementation-in-ios","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/beacon-implementation-in-ios\/","title":{"rendered":"Beacon Implementation in iOS"},"content":{"rendered":"<p class=\"p1\"><b>What is iBeacon<\/b><\/p>\n<p class=\"p1\">iBeacon is a new technology that extends l<span class=\"s1\">ocation services<\/span> in iOS. Beacon is a very generic term but Apple has made a wrapper for this and it knows beacon as iBeacon. iBeacons are very small handy devices that works on the principle of BLE.<\/p>\n<p class=\"p1\"><strong>What is BLE<\/strong><\/p>\n<p class=\"p1\">BLE extends for Bluetooth Low Energy. The small BLE chips are designed in a way to consume very less power\u00a0so that one BLE device can be used for a couple of\u00a0\u00a0years. BLE works on\u00a0central peripheral mode.<\/p>\n<p class=\"p1\"><b>How to create a beacon region and monitor it:\u00a0<\/b><\/p>\n<p class=\"p1\">As we know that in iOS earlier we were using geo-fencing to monitor the coordinates of a geographical region, for that we need to create a circular region by giving the radius of circle. But using iBeacon technology we can create a beacon region by broadcasting the unique packets and when an iOS device receives the packet the application get to know that device entered into the beacon region.<\/p>\n<p class=\"p1\"><strong>We get three callbacks when an iOS device comes into the vicinity of beacon region.<\/strong><\/p>\n<ul>\n<li class=\"p1\">When a device enters the beacon region<\/li>\n<li class=\"p1\">When device exits from\u00a0the beacon region<\/li>\n<li class=\"p1\">Once the device enters into the beacon region the ranging get\u00a0started<\/li>\n<\/ul>\n<p><strong>How to create a packet that we broadcast to create a beacon region:<\/strong> Three parameters play very important role in creating a beacon region.<\/p>\n<p class=\"p3\">1. A proximity UUID helps to create a beacon region. We can generate UUID with the help of following command uuidgen. This command will give you a uuid string. This is the first unique identifier that we can use to create region.<\/p>\n<p class=\"p3\">2. Major value is the second unique identifier that helps to create more deep down region and its a 16 bit unsigned integer.<\/p>\n<p class=\"p3\">3. Minor value is the third\u00a0unique identifier that helps to create more deep down region and its a 16 bit unsigned integer.<\/p>\n<p class=\"p3\"><strong>How to implement iBeacon in iOS using Objective c:<\/strong><\/p>\n<p class=\"p3\">There are two applications required to create Beacon experience.<\/p>\n<ol>\n<li>Transmitter<\/li>\n<li>Receiver<\/li>\n<\/ol>\n<p class=\"p3\"><b>Frameworks required:<\/b><\/p>\n<p>To integrate beacon we need these frameworks.<\/p>\n<ol>\n<li>CoreLocation.framework<\/li>\n<\/ol>\n<p>2. \u00a0 CoreBluetooth.framework<\/p>\n<p><b>Beacon Transmitter<\/b><\/p>\n<p>We can make\u00a0our iPhone device a Beacon transmitter by the help of coding. Before transmitting\u00a0it is \u00a0necessary to know whether the device \u00a0Bluetooth is on or off.<\/p>\n<p><strong>How to check that Bluetooth is on or off in device<\/strong><\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\nif (peripheral.state == CBPeripheralManagerStatePoweredOn)<br \/>\n{<br \/>\n\tNSLog(@&quot;Bluetooth is\u00a0\u00a0on&quot;);<br \/>\n\t\/\/We can advertise the ble packet<br \/>\n}<br \/>\nelse if (peripheral.state == CBPeripheralManagerStatePoweredOff)<br \/>\n{<br \/>\n\tNSLog(@&quot;Bluetooth is \u00a0off&quot;);<br \/>\n}<br \/>\n[\/code]<\/p>\n<p class=\"p3\"><strong>\u00a0How to make your iPhone device a beacon broadcaster:<\/strong><\/p>\n<p>\/*How to create your peripheral data*\/<\/p>\n<p>self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];<\/p>\n<p class=\"p3\">\/*Initializing the PeripheralManager*\/<\/p>\n<p class=\"p3\">self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];<\/p>\n<p class=\"p3\">\/*For broadcasting the data we need to start advertising the data*\/<\/p>\n<p class=\"p3\">[self.peripheralManager startAdvertising:self.beaconPeripheralData];<\/p>\n<p class=\"p3\"><em>After executing this code your iPhone device\u00a0will work as a Beacon hardware.Beacon Receiver<\/em><\/p>\n<p class=\"p3\"><b><b>How to create\u00a0a beacon region to be monitored<\/b><\/b><\/p>\n<p class=\"p3\">To monitor a beacon first we need to create beacon region and register it with the system.This sample code illustrate creation and registration of a beacon region.<\/p>\n<p class=\"p3\">\/\/Create the beacon region<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\nCLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUIDidentifier:identifier];<br \/>\n\/\/Register the beacon region with location manager<br \/>\n[self.locationManager startMonitoringForRegion:beaconRegion];<br \/>\n[\/code]<\/p>\n<p><strong>Start the ranging of the beacon<\/strong><\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n[self.locationManager startRangingBeaconsInRegion:beaconRegion];<br \/>\n[\/code]<\/p>\n<p class=\"p3\"><b>Ranging and Monitoring<\/b><\/p>\n<ul>\n<li><b><b>Region monitoring:<\/b><\/b><\/li>\n<\/ul>\n<p class=\"p3\">When a user enters or exits the registered Beacon region iOS notifies the app by providing call back. But to enable this service we need to set some properties.<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n[beaconRegion setNotifyOnEntry:YES];<br \/>\n[beaconRegion setNotifyOnExit:YES];<br \/>\n[\/code]<\/p>\n<p class=\"p3\">\/*This method gets called when an application enters the Beacon region*\/<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region<br \/>\n{<br \/>\n\t\/\/ See if we&#8217;ve entered the region.<br \/>\n\t\/\/Corresponding operation gets performed.<br \/>\n}<\/p>\n<p>\/*This method get called when an application exit an beacon region*\/<\/p>\n<p>-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region<br \/>\n{<br \/>\n\t\/\/ See if we&#8217;ve exit the region.<br \/>\n\t\/\/Corresponding operation gets performed.<br \/>\n}<\/p>\n<p>-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region<br \/>\n{<br \/>\n\t\/\/ See if we&#8217;ve entered the region.<br \/>\n\t\/\/Corresponding operation gets performed.<br \/>\n}<br \/>\n[\/code]<\/p>\n<p class=\"p3\">\/*This method gets called when an application exits the Beacon region*\/<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region<br \/>\n{<br \/>\n\t\/\/ See if we&#8217;ve exit the region.<br \/>\n\t\/\/Corresponding operation gets performed.<br \/>\n}<br \/>\n[\/code]<\/p>\n<ul>\n<li><b><b>Beacon Ranging:<\/b><\/b><\/li>\n<\/ul>\n<p>When a device enters a Beacon region it provides the beacon related data inside this method as a call back.<\/p>\n<p><b>Note : <\/b>Beacon ranging would not happen in background<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\n-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region<br \/>\n{<br \/>\n\tCLBeacon *beacon = [[CLBeacon alloc] init];<br \/>\n\tbeacon = [beacons lastObject];<br \/>\n\tBeaconInfoModel *modelObj = [[BeaconInfoModel alloc]init];<br \/>\n\tmodelObj.uuid = beacon.proximityUUID.UUIDString;<br \/>\n\tmodelObj.major = beacon.major;<br \/>\n\tmodelObj.minor = \u00a0beacon.minor;<br \/>\n\tmodelObj.accuracy = beacon.accuracy;<br \/>\n\tmodelObj.proximity = beacon.proximity;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p class=\"p3\"><strong><strong>\u00a0<\/strong><\/strong>An iOS device receiving an iBeacon transmission can approximate the distance from the iBeacon. The distance (between transmitting iBeacon and receiving device) is categorized into 3 distinct ranges:<\/p>\n<ul>\n<li>Immediate: Within a few centimetres<\/li>\n<li>Near: Within a couple of meters<\/li>\n<li>Far: Greater than 10 meters away<\/li>\n<\/ul>\n<p>Happy Coding&#8230; \ud83d\ude42<\/p>\n<p><span id=\"hs-cta-wrapper-eb235ec9-785f-4205-9d54-fc88423e9655\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-eb235ec9-785f-4205-9d54-fc88423e9655\" class=\"hs-cta-node hs-cta-eb235ec9-785f-4205-9d54-fc88423e9655\"> <a href=\"http:\/\/insights.tothenew.com\/mobility-beacons-proximity-solutions\"><img decoding=\"async\" id=\"hs-cta-img-eb235ec9-785f-4205-9d54-fc88423e9655\" class=\"hs-cta-img\" style=\"border-width: 0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/481864\/eb235ec9-785f-4205-9d54-fc88423e9655.png\" alt=\"Beacons - Building Proximity based Solutions for Brands\" \/><\/a><br \/>\n<\/span><br \/>\n<script src=\"https:\/\/js.hscta.net\/cta\/current.js\" charset=\"utf-8\"><\/script><script type=\"text\/javascript\">\/\/ <![CDATA[\n hbspt.cta.load(481864, 'eb235ec9-785f-4205-9d54-fc88423e9655'); \n\/\/ ]]><\/script><br \/>\n<\/span><br \/>\n<!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is iBeacon iBeacon is a new technology that extends location services in iOS. Beacon is a very generic term but Apple has made a wrapper for this and it knows beacon as iBeacon. iBeacons are very small handy devices that works on the principle of BLE. What is BLE BLE extends for Bluetooth Low [&hellip;]<\/p>\n","protected":false},"author":236,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1400,1],"tags":[2184],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25348"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/236"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=25348"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25348\/revisions"}],"predecessor-version":[{"id":59887,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25348\/revisions\/59887"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=25348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=25348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=25348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}