读书笔记 --- 定位(iBeacon)

1.iBeacon基站


#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

#define kUUID @"AF56BA9A-A3CD-4FA2-8908-84343271439E"//用来表示iBeacon的UUID
#define kID @"com.51work6.AirLocate"//用来标识地理围栏区域
#define kPower  @-59//蓝牙信号强度

@interface ViewController : UIViewController <CBPeripheralManagerDelegate>

@property (strong, nonatomic) CBPeripheralManager  *peripheralManager;

- (IBAction)valueChanged:(id)sender;

@end




@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _peripheralManager= [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];//创建一个外设管理类
}


- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
    NSLog(@"外设状态变化");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)valueChanged:(id)sender {
    
    UISwitch* swc = (UISwitch* )sender;
    
    if (swc.isOn) {
        
        NSUUID* uuid = [[NSUUID alloc] initWithUUIDString:kUUID];
        
        //创建CLBeaconRegion 对象,CLBeaconRegion类描述了基于蓝牙低功耗Beacon的地理围栏区域.CLBeaconRegion是时限为定位的核心类,创建CLBeaconRegion对象关联到4个属性
        /**
         proximityUUID 这是一个128位的唯一标示,表示一个或多个iBeacon基站.这个属性是必须的
         identifier 这是用来标识地理围栏区域
         major(主值)它是一个16位的无符号整数,用于区分有相同的接近UUID的iBeacon基站
         manor(副值)它是一个16位的无符号整数,用于区分有相同的接近UUID的主值iBeacon基站
         */
        CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:kID];
        
        NSDictionary *peripheralData = [region peripheralDataWithMeasuredPower:kPower];
        
        if(peripheralData) {
            [_peripheralManager startAdvertising:peripheralData];
        }
    } else {
        [_peripheralManager stopAdvertising];
    }
}



@end



2.客户端

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

#define kUUID @"AF56BA9A-A3CD-4FA2-8908-84343271439E"
#define kID @"com.51work6.MicLocate"
#define kPower  @-59

@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager  *locationManager;
@property (strong, nonatomic) CLBeaconRegion *region;

@property (weak, nonatomic) IBOutlet UILabel *lblRanging;

- (IBAction)onClickMonitoring:(id)sender;

- (IBAction)rangValue:(id)sender;

@end



@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    
    
    NSUUID* uuid = [[NSUUID alloc] initWithUUIDString:kUUID];
    _region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:kID];//通过UUID和ID属性实例化CLBeaconRegion对象
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    [_locationManager stopMonitoringForRegion:_region];//停止测试范围
    [_locationManager stopRangingBeaconsInRegion:_region];//停止距离检测
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)onClickMonitoring:(id)sender {
    [_locationManager startMonitoringForRegion:_region];//开始测试范围
}


- (IBAction)rangValue:(id)sender {
    UISwitch* swc = (UISwitch* )sender;
    if (swc.isOn) {
        [_locationManager startRangingBeaconsInRegion:_region];
    } else {
        [_locationManager stopRangingBeaconsInRegion:_region];
    }
    
}

//范围状态发送变化的事后回调该方法
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    if(state == CLRegionStateInside) {
        notification.alertBody = @"你在围栏内";
    } else if(state == CLRegionStateOutside) {
        notification.alertBody = @"你在围栏外";
    } else {
        return;
    }
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

//设备进入到指定围栏内时候毁掉该方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"你进入围栏";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

//设备退出指定围栏时候回调该方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"你退出围栏";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    
}

//在指定的围栏中Beacon设备位置变化时候回调方法
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    NSArray *unknownBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityUnknown]];
    if([unknownBeacons count])
        _lblRanging.text = @"未检测到";
    
    NSArray *immediateBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityImmediate]];
    if([immediateBeacons count])
        _lblRanging.text = @"最接近";
    
    NSArray *nearBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityNear]];
    if([nearBeacons count])
        _lblRanging.text = @"近距离";
    
    NSArray *farBeacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity = %d", CLProximityFar]];
    if([farBeacons count])
       _lblRanging.text = @"远距离";
}


@end



更多干货,请支持原作:http://item.jd.com/11436547.html

你可能感兴趣的:(ios,读书笔记,定位,ibeacon)