Skip to content

Instantly share code, notes, and snippets.

@hongseok
Created April 1, 2016 07:06
Show Gist options
  • Save hongseok/10421360f8ef373f54f6407fc8e45500 to your computer and use it in GitHub Desktop.
Save hongseok/10421360f8ef373f54f6407fc8e45500 to your computer and use it in GitHub Desktop.
iBeacon Advertisement Example
//
// ViewController.m
//
// Created by nhn on 2016. 4. 1.
// Copyright © 2016 HONGSEOK. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
static int broadcastpower = -59;
NSString *const ProximityUUID = @"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0";
@interface ViewController () <CBPeripheralManagerDelegate>
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
@property (assign, nonatomic) BOOL isBluetoothEnabled;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
// Start Beacon Advertisement
- (void)startBeaconAdvertise:(CLBeaconMajorValue)major minor:(CLBeaconMinorValue)minor key:(NSString *)key {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:ProximityUUID];
CLBeaconRegion *newregion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@"COM.SELF.ID"];
NSMutableDictionary *peripheralData = [newregion peripheralDataWithMeasuredPower:@(broadcastpower)];
NSLog(@"start advertising %@",peripheralData);
//Advertise the same beacon region and Range the same beacon region
[self.peripheralManager startAdvertising:peripheralData];
}
-(void)stopBeaconAdvertise{
if (self.peripheralManager) {
[self.peripheralManager stopAdvertising];
}
}
// Stop Beacon Advertisement
#pragma mark - CBPeripheralManagerDelegate
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
NSLog(@"peripheral %@",peripheral);
//Check if the BLE state was on or not
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
self.isBluetoothEnabled = YES;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment