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).
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.
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:
So after we will get call in delegate methods of core location.
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.
Transmitter.m
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:
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.
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
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;
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.
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
Transmitter.h
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
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.
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];
}
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
Post a Comment