[Obj-C] How to connect to beacons? A simple tutorial

Hello Community,

Here’s a tutorial how to create a simple beacon app which connects to your beacon. I’m not an iOS developer and even I could describe myself as an Obj-C noob. But I’ve managed to finish it. If I can do that, you also can. Just follow these steps:

  1. Make sure you’re a part of Apple’s iOS Developer Program, without it you won’t be able to build the app on you iOS mobile device.

  2. Create a Mobile App in the Estimote Cloud to get you App ID and App Token.

  3. Download Estimote SDK from Github.

  4. Open Xcode and create new project. In templates choose iOS Application and Single View Application. Click on the Next button and name your app. After that select a directory in which you want to save your project and press ‘Create’.

  5. We need to add the SDK framework. Open zip with our EstimoteSDK, go to EstimoteSDK folder and drag&drop ‘EstimoteSDK.framework’ file below your app’s files. Here’s where the coding begins! :slight_smile:

  6. Go to AppDelegate.m file and import EstimoteSDK. Just below "AppDelegate.h" insert: #import <EstimoteSDK/EstimoteSDK.h>

  7. Between the@interface and @end section insert our global property @property (nonatomic, strong) ESTBeaconConnection *beaconConnection;

  8. Below

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

Insert [ESTCloudManager setupAppID:@"x" andAppToken:@"y"]; code, where x and y are your app’s credentials from the Estimote Cloud. Thanks to that the app will be able to check if the beacon is yours.
9. After that lets create a beaconConnection object with:

self.beaconConnection = [[ESTBeaconConnection alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID major:57459 minor:47724 delegate:self startImmediately:YES];

where you give UUID/major/minor details or

self.beaconConnection = [[ESTBeaconConnection alloc]initWithMacAddress:@"mac" delegate:self startImmediately:YES]

where you declare a beacon with its MAC address (all lowercase and with no colons e.g. “ec4510be5562”).
As the code executes, it starts of connection immediately. What does delegate:self mean? Basically we tell the app to send all messages to us.

10.Congratulations, you’ve just started connection. Now we need to make sure it followed through. Below the first } implement this:

- (void)beaconConnection:(ESTBeaconConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Ups, something went wrong. Error: %@", error); 

This method will inform us when the connection fails and will tell us what happened.

11.More interesting part starts when we succeed:

- (void)beaconConnectionDidSucceed:(ESTBeaconConnection *)connection {
    NSLog(@"Connection succeed");

12.Yuhooo!! Now we can do tons of cool things (insert the code below our NSLog message).

To get the battery level:

NSNumber *batteryLevel = [self.beaconConnection batteryLevel];
NSLog(@"Battery level in this beacon %@", batteryLevel);

To read the temperature:

    [self.beaconConnection readTemperatureWithCompletion:^(NSNumber* value, NSError *error) {
        if (!error)
            NSLog(@"Temperature in the room: %@", value);

Or even to change the advertising interval:

[self.beaconConnection writeAdvInterval:100 completion:^(unsigned short value, NSError *error) {
        if (!error) {
            NSLog(@"Changed! Current advertising interval: %d", value);
        } else {
            NSLog(@"Error: %@", error);

13.Don’t forget to add [self.beaconConnection disconnect]; to disconnect from the beacon.
14. Connect your mobile device with a cable and build your app with ⌘B or with a play icon in the top of the Xcode Toolbar.

And we’ve finished. You’ve just created an app that allows you to connect to a beacon and read/write some basic values. Congratulations!


Hi Witek !

I need to do this but in another THREAD, not in the MAIN thread. I try with this:

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
     self.beaconConnection = [[ESTBeaconConnection alloc]initWithMacAddress:@"mac" delegate:self startImmediately:YES]

but DELEGATE methods doesn`t response. Can you help me ?