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

Constant Weds Literals Venue Objective C

Arrangement: While working on a application you may have used some predefined values. The visibility of these values can be application level, feature level or class level. For the  convenience  we give name to these values and then use in the application. In this case the name given by you only representing the single value and it never change to represent some other value thats why it is called Constant. On the other hand variables are like us doing jobs in IT World and can switch from one organization( value ) to other. Like while choosing a partner for marriage you may choose best for you by seeing multiple persons and still after sometime you feel stuck. Same may happen you choose a best option to declare a constant and after sometime this constant can create a issue for you in code due to its scope or any other reason. On the other hand you may have a fixed mind that you always declare a constant in a same way in any situation like a love ma...

Implement orientation modes in iPhone Hybrid Applications

Let suppose you are working on a hybrid application which runs only in single (portrait) mode. One day a requirement come that PDF and Doc Viewer (HTML Page) should support both (landscape, portrait) mode. Your Application loads all the HTML content from the local html files and you need to implement the above functionality only for one HTML file. Let break the above task in the modules: Step 1: Application should detect when the PDF and Doc viewer is open in application. I setup location.href tag in html to " docvieweron:// " and " docvieweroff:// " when page is open and closed respectively. In this way I am getting a delegate callback in web view: WebViewDelegate: - ( BOOL ) webView:( UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request   navigationType: ( UIWebViewNavigationType )navigationType {          NSString * urlString = [[request URL ] absoluteString ];...

Best Practices in iOS with Objective - C

In this post i am making collection of points which i read from some other post or some documentation. Today we will learn what are good or bad habits while doing programming in objective - c. 1) Instance Type and id I noticed one day that in iOS SDK init method of class has  instancetype  as a return value. I am curious to know about this thing and why this type not id. So what i found is following: Example: - ( instancetype )initWithProximityUUID:( NSUUID *)proximityUUID identifier:( NSString *)identifier What is instancetype? Suppose you are writing a method for object initialisation and according to objective c coding convention you need to return the object of same class from the method. So you can use instance type as a return type of method. Instance Type -  It represents the instance of the class or subclass of the class in which you are writing the method with return type instancetype. Confusing ? Let suppose i have declare a class empl...