1. Two iPhone4s are used to simulate the iBeacons
2. uuidgen is used using the Terminal for generating the UUIDString
Broadcast:
==========
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : UIViewController<CBPeripheralManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) NSDictionary *myBeaconData;
@property (strong, nonatomic) CBPeripheralManager *peripheralManager;
@end
-------------------------------------------------------------------------
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a NSUUID object
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"A77A1B68-49A7-4DBF-914C-760D07FBB87B"];
// Initialize the Beacon Region
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
major:1
minor:1
identifier:@"com.appcoda.testregion"];
}
- (IBAction)buttonClicked:(id)sender {
// Get the beacon data to advertise
self.myBeaconData = [self.myBeaconRegion peripheralDataWithMeasuredPower:nil];
// Start the peripheral manager
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:nil];
}
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral
{
if (peripheral.state == CBPeripheralManagerStatePoweredOn)
{
// Bluetooth is on
// Update our status label
self.statusLabel.text = @"Broadcasting...";
// Start broadcasting
[self.peripheralManager startAdvertising:self.myBeaconData];
}
else if (peripheral.state == CBPeripheralManagerStatePoweredOff)
{
// Update our status label
self.statusLabel.text = @"Stopped";
// Bluetooth isn't on. Stop broadcasting
[self.peripheralManager stopAdvertising];
}
else if (peripheral.state == CBPeripheralManagerStateUnsupported)
{
self.statusLabel.text = @"Unsupported";
}
}
Receiver's:
===========
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@end
-------------------------------------------------------------------------
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"A77A1B68-49A7-4DBF-914C-760D07FBB87B"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"com.appcoda.testregion"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
//-------------- Create LocalNotification to alert User ------------------
UILocalNotification *notificaiton = [[UILocalNotification alloc] init];
notificaiton.alertBody = @"Welcome User";
notificaiton.soundName = @"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notificaiton];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
self.beaconFoundLabel.text = @"No";
//-------------- Create LocalNotification to alert User ------------------
UILocalNotification *notificaiton = [[UILocalNotification alloc] init];
notificaiton.alertBody = @"GoodBye";
notificaiton.soundName = @"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notificaiton];
}
-(NSString *)nameForProximity:(CLProximity)proximity {
switch(proximity) {
case CLProximityUnknown:
return @"Unknown";
break;
case CLProximityImmediate:
return @"Immediate";
break;
case CLProximityNear:
return @"Near";
break;
case CLProximityFar:
return @"Far";
break;
}
}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
if(beacons.count) {
// Beacon found!
self.statusLabel.text = @"Beacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
NSLog(@"Message : %@", [self nameForProximity: foundBeacon.proximity);
// You can retrieve the beacon data from its properties
//NSString *uuid = foundBeacon.proximityUUID.UUIDString;
//NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
//NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
} else {
NSLog(@"No beacons found");
}
}
No comments:
Post a Comment