Skip to main content

Everything about BLE with iOS Part2 (Implementation)

Today we will learn how can we implement a BLE receiver and a BLE transmitter with iOS SDK. For this we will use both the SDK describe in part1.

CoreLocation :  We will use for implementing the central which accept the data from the peripheral.
CoreBluetooth : We will use for implementing the peripheral (has data to send).

Central:

It is device who can receive data from the peripheral if it knows peripheral proximity UUID of the peripheral. So if you have an iBeacon then seller must provide you proximity UUID for the beacon.  For listening to iBeacon you must initialize a beacon region with the proximity UUID. API for initialize the region is in core location framework:

- (instancetype)initWithProximityUUID:(NSUUID *)proximityUUID identifier:(NSString *)identifier


There are multiple variant of this method for specifying major and minor value with the UUID. You can select based on your requirement. Here for the example purpose we are not grouping the iBeacon so we do not need to specify major and minor value.

We have specified that we need to monitor the region or the location of peripheral. It is similar to that we need to monitor the GPS location of device.  In case of GPS we need core location class to monitor the location. Similarly for sensing the location of peripheral we need a core location frame work.

Our next step will be initialize the core location object and implement its delegate.

initialization:

Reciver.h

@property (strong, nonatomic) CLLocationManager *locationManager

Reciver.m

@synthesize locationManager;

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

So we have initialized the core location object. So now it will monitor the peripheral location. wait?
Can it monitor the  peripheral region ? How? we did not specify anything about peripheral here?

So we miss the point. We need to tell the core location object to which peripheral it has to listen:

[self.locationManager startMonitoringForRegion:self.beaconRegion];

So after we will get call in delegate methods of core location.

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

Our main focus should be on the method didRangeBeacons delegate.


We will get all the beacons list in the array argument we can loop through the list and find the proximity of beacon and other detail.

If we implement all these things then we are ready with the central part.

Peripheral:

We are ready with the central part. If you have iBeacon then you do not need to implement the peripheral part. If you want to turn iPhone as a peripheral then you should have a spare iPhone. 

First step is to create the beacon region and API is same as central to create a region. The only difference is central create a region for listening and peripheral to advertise the packets.

Transmitter.h
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;

Transmitter.m
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:UUID_BEACON
                                                                major:1
                                                                minor:1

                                                           identifier:PROXIMITY_IDENTIFIER];
Note:
UUID_BEACON :128 bit id you can specify of your choice For Ex 293F6664-A3D0-5E71-969D-7364C11A6186.

PROXIMITY_IDENTIFIER : It is identifier for the proximity of your iBeacon. You can specify of your choice and you will get this identifier when you enter to region of peripheral and delegate didEnterRegion will called in central application. 

Major and Minor: For this sample we do not need multiple peripheral so do not need to specify major or minor.

Next you need to create peripheral data which use by peripheral for advertising. iOS SDK provide a easy method for that:

Tranmitter.h
@property (strong, nonatomic) NSDictionary *beaconPeripheralData;
Tranmitter.m
self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];

So peripheralDataWithMeauredPower has an argument which ask for RSSI value. It is an optimal argument so we are specifying here nil.

We are ready with region and peripheral data. Let's create a peripheral manager and start advertising.

Tranmitter.h
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
Tranmitter.m
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
                                                                     queue:nil
                                                                   options:nil];
As soon as you specify delegate for peripheral manager it will try to run the peripheral and you will get call in delegate method of peripheral.

We need to implement the delegate method of peripheral as soon as the peripheral is on we need to start advertising.

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;

We need to check the state of peripheral :
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
   // Start Advertising.
   [self.peripheralManager startAdvertising:self.beaconPeripheralData];

So we are done with the peripheral and central implementation of BLE. In next part we will learn how we can create Service and Characteristics of peripheral and how we can listen those services.

You can find full source code of today tutorial at this link.

Source code contain the implementation for Receiver and transmitter. You need to initialise the respective object with specify proximity UUID and Identifier.

Comments

Popular posts from this blog

What does enable bitcode do in Xcode

Background: Now days compilation process for any language is divided into two parts and same is applicable for objective c. Frontend Compiler (Clang) Backend Compiler (LLVM) Frontend Compiler (Clang):  Responsibility of front-end compiler is to take a source code and convert into intermediate representation (IR).  In case of clang it is LLVM IR.  Backend Compiler(LLVM):  Responsibility of backend compiler is to take a IR as input and convert into object code. LLVM input is bitstream of LLVM IR (Bitcode) and output is sequence of machine instruction(Object code). Each  cpu or processor has different set of  M achine   instruction, So  LLVM output is CPU dependent or it can be executed on specific CPU only.   There may be question in your mind that  1) What is the need to divide into these phases? 2) What is LLVM IR? Can we see the LLVM IR as Output? What is the need to divide into these phases? It is beneficial for both the programming language designer a

Asynchronous Request with NSOperationQueue

Today post is about how to run a asynchronous task in NSOperationQueue.  Generally we do not run a Asynchronous task in NSOperationQueue. It is also not recommended for any programmer to do that. This post is only for learning purpose what will happen if we schedule a asynchronous task in a queue and how can we complete that task:). So let us move to the learning: NSOperationQueue: In iOS NSOperationQueue is a class, which provide a way to perform operation concurrently. We also have others way to perform concurrent operation: 1) GCD 2) NSThread 3) pThread NSOperationQueue is a wrapper on GCD, which provides a very convenient way to execute operation concurrently. To create a Queue for Operation you have to simply allocate a object of the class: NSOperationQueue * opertionQueue = [[ NSOperationQueue alloc ] init ]; For this post let suppose you are making a queue to handle all Http request in your application. So i want to create a queue in Handler class

Shake Effect in iOS

Animation Animation always capture the user attention. We can use animation to update things on the screen.  For developer also animations fascinated things to learn and implement. Today we will try shake effect with Various  API. CABasicAnimation: Here we animate view's frame y coordinate from one position to another position. It is simple example of changing the position with respect to time. CABasic Animation is deal with the single keyframe.                                        y(t) = y 0 + t*d(y) You can use CABasic Animation if you have to play with single value. You need to move object from  one position to another without any intermediate steps. CABasicAnimation * shakeAnimation = [CABasicAnimation animationWithKeyPath: @ "position" ]; shakeAnimation . duration = 0.05 ; shakeAnimation . autoreverses = YES; shakeAnimation . repeatCount = 6 ; CGPoint shakeFromPoint = CGPointMake( self . shakeLabel . center . x,