Created
May 1, 2020 10:11
-
-
Save paulw11/382c35b8cc857c0dfa1fc78070f5244d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ConfigViewController.m | |
// beacon | |
// | |
// Created by Paul Wilkinson on 1/5/20. | |
// Copyright © 2020 Paul Wilkinson. All rights reserved. | |
// | |
#import "ConfigViewController.h" | |
#import <CoreLocation/CoreLocation.h> | |
#import <CoreBluetooth/CoreBluetooth.h> | |
@interface ConfigViewController() | |
@end | |
@implementation ConfigViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
[self initBeacon]; | |
[self setLabels]; | |
} | |
- (void)initBeacon { | |
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"EC1309B2-9736-40FF-B255-7EE047F2166B"]; | |
self.beaconRegion = [[CLBeaconRegion alloc] initWithUUID:uuid major:1 minor:1 identifier:@"com.devfright.myRegion"]; | |
self.beaconPeripheralData =[self.beaconRegion peripheralDataWithMeasuredPower:nil]; | |
self.peripheralManager =[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil]; | |
} | |
- (void)setLabels { | |
self.uuidLabel.text = self.beaconRegion.UUID.UUIDString; | |
self.majorLabel.text = [NSString stringWithFormat:@"%@", self.beaconRegion.major]; | |
self.minorLabel.text = [NSString stringWithFormat:@"%@", self.beaconRegion.minor]; | |
self.identityLabel.text = self.beaconRegion.identifier; | |
} | |
- (IBAction)transmitBeacon:(UIButton *)sender { | |
[self.peripheralManager startAdvertising:self.beaconPeripheralData]; | |
} | |
-(void) peripheralManagerDidUpdateState: | |
(CBPeripheralManager *)peripheral { | |
NSLog(@"peripheralManagerDidUpdateState:"); | |
switch (peripheral.state) { | |
case CBManagerStatePoweredOff: | |
NSLog(@"CBManagerStatePoweredOff"); | |
break; | |
case CBManagerStateResetting: | |
NSLog(@"CBManagerStateResetting"); | |
break; | |
case CBManagerStatePoweredOn: | |
NSLog(@"CBManagerStatePoweredOn"); | |
//---start advertising the beacon data--- | |
break; | |
case CBManagerStateUnauthorized: | |
NSLog(@"CBManagerStateUnauthorized"); | |
break; | |
case CBManagerStateUnsupported: | |
NSLog(@"CBManagerStateUnsupported"); | |
break; | |
default: | |
NSLog(@"CBPeripheralStateUnknown"); | |
break; | |
} | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TrackViewController.m | |
// beacon | |
// | |
// Created by Paul Wilkinson on 1/5/20. | |
// Copyright © 2020 Paul Wilkinson. All rights reserved. | |
// | |
#import "TrackViewController.h" | |
@interface TrackViewController () | |
@end | |
@implementation TrackViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
self.locationManager = [[CLLocationManager alloc] init]; | |
self.locationManager.delegate = self; | |
//---create a NSUUID object--- | |
NSUUID *proximityUUID =[[NSUUID alloc] initWithUUIDString:@"EC1309B2-9736-40FF-B255-7EE047F2166B"]; | |
self.beaconRegion =[[CLBeaconRegion alloc]initWithUUID:proximityUUID | |
identifier:@"net.learn2develop.myRegion"]; | |
//---start monitoring a region--- | |
[self.locationManager requestAlwaysAuthorization]; | |
[self.locationManager startMonitoringForRegion:self.beaconRegion]; | |
} | |
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { | |
//---start ranging for iBeacons--- | |
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion]; | |
self.beaconFoundLabel.text = @"Entered region"; | |
// UILocalNotification *localNotification =[[UILocalNotification alloc] init]; | |
//---the message to display for the alert--- | |
// localNotification.alertBody =@"You have entered the region you are monitoring"; | |
//---uses the default sound--- | |
// localNotification.soundName =UILocalNotificationDefaultSoundName; | |
//---title for the button to display--- | |
//localNotification.alertAction = @"Details"; | |
//---schedule the notification--- | |
// [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification]; | |
} | |
-(void)locationManager:(CLLocationManager *)manager | |
didExitRegion:(CLRegion *)region { | |
//---stop ranging for iBeacons--- | |
self.beaconFoundLabel.text = @"Exit region"; | |
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion]; | |
} | |
//---when iBeacons are found--- | |
-(void)locationManager:(CLLocationManager *)manager | |
didRangeBeacons:(NSArray *)beacons | |
inRegion:(CLBeaconRegion *)region { | |
CLBeacon *beacon = [beacons lastObject]; | |
self.beaconFoundLabel.text = @"Yes"; | |
self.proximityUUIDLabel.text = beacon.UUID.UUIDString; | |
self.majorLabel.text =[NSString stringWithFormat:@"%@", beacon.major]; | |
self.minorLabel.text = | |
[NSString stringWithFormat:@"%@", beacon.minor]; | |
self.accuracyLabel.text =[NSString stringWithFormat:@"%f meters", beacon.accuracy]; | |
switch (beacon.proximity) { | |
case CLProximityUnknown: | |
self.distanceLabel.text = @"Unknown Proximity"; | |
break; | |
case CLProximityImmediate: | |
self.distanceLabel.text = @"Immediate"; | |
break; | |
case CLProximityNear: | |
self.distanceLabel.text = @"Near"; | |
break; | |
case CLProximityFar: | |
self.distanceLabel.text = @"Far"; | |
break; | |
} | |
self.rssiLabel.text =[NSString stringWithFormat:@"%li decibels", | |
(long)beacon.rssi]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment