DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Configuring SSO Using WSO2 Identity Server
  • Deploy MuleSoft App to CloudHub2 Using GitHub Actions CI/CD Pipeline
  • 4 Email Validation and Verification Techniques for App Developers
  • Boosting Efficiency With Cross-Platform Code-Sharing Techniques

Trending

  • OneStream Fast Data Extracts APIs
  • Vector Database: A Beginner's Guide
  • Understanding Git
  • What Is Good Database Design?

Using and Creating Beacons in iOS 7

Josh Anderson user avatar by
Josh Anderson
·
Oct. 09, 13 · Interview
Like (0)
Save
Tweet
Share
5.93K Views

Join the DZone community and get the full member experience.

Join For Free

This post was originally written by Stephen Zaharuk.

One of the many cool features introduced into iOS 7 is the concept of Beacons. The idea behind them is that you can have an iOS device, or even a stand-alone device, broadcast a beacon. And then within an iOS 7 application, you can listen for that beacon and determine if you're within range. If so, then you can display contextual information to your end user. 

For example, MLB is supposedly going to use this technology in their stadiums to provide directions to your seats or even provide your coupons/deals. 

Pretty cool, right? If I've peaked your interest, read on and I'll explain exactly how to add this to your own application. You may be surprised how little code is necessary!

So the first thing you need to do is add the following frameworks to your project:

  • CoreLocation.framework
  • CoreBluetooth.framework
The bluetooth framework is actually only necessary if you plan on using your app to host a beacon.
Step 1:

First, we need to generate a UUID (Universally Unique Identifier). This identifier will be used by both the beacon and your app, so that they can identify each other and establish a connection. 

There are two ways to do this. 

  1. Use the uuidgen command-line tool that comes with Xcode. 
  2. Just write some code to generate one. Specifically, just use the following method. And then store if off for use in your app. [NSUUID UUID]

Step 2:

Now that you have your UUID, you can create an instance of your region:

NSUUID* uuid = [[NSUUID alloc]initWithUUIDString:@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"]; 

_region = [[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:@"myApp"];

The first line creates an instance of your UUID. (Note: you need to replace the string above with your actual UUID, otherwise the uuid returned will be nil)

The next line creates your region. You must pass in a valid UUID and an identifier. The identifier can be any string, but you should make it useful. Especially if your app will handle multiple regions. For example, imagine you were writing an app for a Mall. You could have a region for each store in the mall. So in this case, your identifier would be a unique string that identifies a particular store. 

Step 3:

For this step, let's pretend we're already set up a beacon, whether it be another iOS device or an independent bluetooth transmitter. And our app wants to listen for that beacon. Assuming that the beacon is broadcasting using our same UUUID, we just need to use the following code:

_lm = [[CLLocationManager alloc]init];

_lm.delegate = self;

[_lm startRangingBeaconsInRegion:_region];

We first create a Location Manager, assign its delegate, and finally call "startRangingBeaconsInRegion:" That last part tells the app that it should start listening for our specific region that we created in Step 2.

The delegate we assigned is of the type CLLocationManagerDelegate, and we just need to listen to one particular method:

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region

{

}

Once we've entered within the beacon's range, this method will fire constantly, providing us with all the beacons within our range. You can then loop through each beacon that was returned and determine:

  • Its accuracy in meters
  • Its signal strength
  • And finally, its proximity (Far, Near, Immediate)
You can then use this information in your app to figure out what you want to display to the user. For example, if we use the mall example we used before, you could determine which store you're in front of, and then provide the user with coupons or promote new items for sale. 
Step 4

This next step is optional. I'll show you how to make your app become an iOS 7 beacon. (Note: this is why we needed to add the CoreBluetooth.framework)

In order to do this, we first need to create a Peripheral Manager:

_pm = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

The manager takes two parameters. The first is the delegate of type CBPeripheralManagerDelegate and the next is the thread in which it should run (we pass in nil so that it runs on the main thread).

For the delegate, we only have to implement one method. This method lets us know when the manager has been "powered on."

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral

{

    if(peripheral.state ==  CBPeripheralManagerStatePoweredOn)

    {

        NSDictionary* values = [_region peripheralDataWithMeasuredPower:nil];

        [peripheral startAdvertising:values];

    }

}

Once we know we're on, we can use our region, the same region we created in Step 2, to start advertising our beacon. 

And just like that, you've created a working beacon!

Enjoy!

app IOS 7

Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Configuring SSO Using WSO2 Identity Server
  • Deploy MuleSoft App to CloudHub2 Using GitHub Actions CI/CD Pipeline
  • 4 Email Validation and Verification Techniques for App Developers
  • Boosting Efficiency With Cross-Platform Code-Sharing Techniques

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: