How to Manage iBeacon Scanning in iOS using Mesosfer – Part 2
This tutorial is about How to Manage iBeacon Scanning in iOS using Mesosfer.
Table of Contents
1. Tutorial
This tutorial was the 2nd part of How to Manage iBeacon Scanning in iOS using Mesosfer.
1.1. Define beacon cloud data
Go to your Mesosfer app -> Base -> Beacon, add this beacon below :
1 2 3 4 5 6 |
{ "uuid": "CB10023F-A318-3394-4199-A8730C7C1AEC", "major": 1024, "minor": 2048, "name": "Beacon 1" } |
You can also define your own beacon parameters to match beacon device you have.
1.2. Preparation
Before downloading beacons data, we need to setup some property :
CBBluetoothManager
. This is the entry point to the bluetooth role management like checking bluetooth state, check whether the device is capable of using Bluetooth Low Energy, showing dialog to turn on the bluetooth, etc.CBBeaconManager
. This is the entry point to beacon scanning service like checking authorization status, setting region for scanning, starting and stopping ranging beacons, monitoring beacons, etc.- We need to implement
CBBluetoothManagerDelegate
andCBBeaconManagerDelegate
too.
Add these lines below to the interface of AppDelegate
:
1 2 3 4 5 6 7 8 9 10 |
#import "AppDelegate.h" #import <Cubeacon/Cubeacon.h> #import <Mesosfer/Mesosfer.h> @interface AppDelegate () <CBBluetoothManagerDelegate, CBBeaconManagerDelegate> @property (nonatomic, strong) CBBluetoothManager *bluetoothManager; @property (nonatomic, strong) CBBeaconManager *beaconManager; @end |
Initialize bluetooth and beacon manager within method application:didFinishLaunchingWithOptions
:
1 2 3 4 5 6 |
// initialize bluetooth manager self.bluetoothManager = [[CBBluetoothManager alloc] initWithDelegate:self]; // initialize beacon manager self.beaconManager = [[CBBeaconManager alloc] init]; self.beaconManager.delegate = self; |
After initializing bluetooth manager and set the delegate, we need to implement the method :
1 2 3 4 5 6 7 |
#pragma mark - BLuetooth manager delegate methods - (void)bluetoothManagerDidUpdateState:(CBBluetoothManager *)manager { if (manager.state == CBBluetoothStatePoweredOn) { [self.beaconManager requestAlwaysAuthorization]; } } |
These line above mean that when bluetooth state is already ON, it will call in beacon manager to request authorization to use the location service. As we know that, beacon scanning services are within iOS Core Location Framework. So we need to enable location service and request authorization to use it.
1.3. Download and scan beacon
After authorizing the request for a location service use, the bluetooth manager will call the delegate method didChangeAuthorizationStatus
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
- (void)didChangeAuthorizationStatus:(CBAuthorizationStatus)status { // start download beacon when authorized if (status == CBAuthorizationStatusAuthorizedAlways || status == CBAuthorizationStatusAuthorizedWhenInUse) { // querying all beacon [[MFBeacon query] findAsyncWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) { // check if there is an exception happen if (error) { NSLog(@"Error when downloading beacons: %@", error); return; } // get the result if (objects) { // populate the result into list of region for scanning NSMutableArray *regions = [[NSMutableArray alloc] init]; for (MFBeacon *beacon in objects) { NSLog(@"Beacon: %@", beacon.dictionary); CBRegion *region = [[CBRegion alloc] initWithProximityUUID:beacon.proximityUUID major:[beacon.major intValue] minor:[beacon.minor intValue] identifier:beacon.objectId]; [regions addObject:region]; } // start monitoring using regions [self.beaconManager startMonitoringForRegions:regions]; } else { NSLog(@"Beacon not found."); } }]; } } |
So now we can download beacon data from Mesosfer Cloud and then start the monitoring for beacon regions.
1.4. Manage scanning results
Having the list pre-sorted by the Cubeacon SDK, and with all the prep work we’ve performed, the monitoring listener turns out to be quite simple :
1 2 3 4 5 6 7 8 9 10 11 |
- (void)didEnterRegion:(CBRegion *)region { NSLog(@"Entering region: %@", region); } - (void)didExitRegion:(CBRegion *)region { NSLog(@"Exiting region: %@", region); } - (void)didDetermineState:(CBRegionState)state forRegion:(CBRegion *)region { NSLog(@"Change state %hhu for region: %@", state, region); } |
The complete code for AppDelegate
class :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#import "AppDelegate.h" #import <Cubeacon/Cubeacon.h> #import <Mesosfer/Mesosfer.h> @interface AppDelegate () <CBBluetoothManagerDelegate, CBBeaconManagerDelegate> @property (nonatomic, strong) CBBluetoothManager *bluetoothManager; @property (nonatomic, strong) CBBeaconManager *beaconManager; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // initialize mesosfer sdk [Mesosfer initializeWithApplicationId:@"YOUR-MESOSFER-APP-ID" clientKey:@"YOUR-MESOSFER-CLIENT-KEY"]; // initialize cubeacon sdk [Cubeacon initialize]; // initialize bluetooth manager self.bluetoothManager = [[CBBluetoothManager alloc] initWithDelegate:self]; // initialize beacon manager self.beaconManager = [[CBBeaconManager alloc] init]; self.beaconManager.delegate = self; return YES; } #pragma mark - Beacon manager delegate methods - (void)didEnterRegion:(CBRegion *)region { NSLog(@"Entering region: %@", region); } - (void)didExitRegion:(CBRegion *)region { NSLog(@"Exiting region: %@", region); } - (void)didDetermineState:(CBRegionState)state forRegion:(CBRegion *)region { NSLog(@"Change state %hhu for region: %@", state, region); } - (void)didChangeAuthorizationStatus:(CBAuthorizationStatus)status { // start download beacon when authorized if (status == CBAuthorizationStatusAuthorizedAlways || status == CBAuthorizationStatusAuthorizedWhenInUse) { // querying all beacon [[MFBeacon query] findAsyncWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) { // check if there is an exception happen if (error) { NSLog(@"Error when downloading beacons: %@", error); return; } // get the result if (objects) { // populate the result into list of region for scanning NSMutableArray *regions = [[NSMutableArray alloc] init]; for (MFBeacon *beacon in objects) { NSLog(@"Beacon: %@", beacon.dictionary); CBRegion *region = [[CBRegion alloc] initWithProximityUUID:beacon.proximityUUID major:[beacon.major intValue] minor:[beacon.minor intValue] identifier:beacon.objectId]; [regions addObject:region]; } // start monitoring using regions [self.beaconManager startMonitoringForRegions:regions]; } else { NSLog(@"Beacon not found."); } }]; } } #pragma mark - BLuetooth manager delegate methods - (void)bluetoothManagerDidUpdateState:(CBBluetoothManager *)manager { if (manager.state == CBBluetoothStatePoweredOn) { [self.beaconManager requestAlwaysAuthorization]; } } @end |
It’s done. We will continue this tutorial on Part-3.