본문 바로가기

iOS

iBeacon api 사용법

1. 사용되는 프레임워크

 - CoreBluetooth.framework

 - CoreLocation.framework

 

2. 비콘수신을 위한 준비

 - iOS8버전이후에는 위치정보 사용하는 방법이 2가지로 되었다.

  프로젝트 info에 NSLocationWhenInUseUsageDescription/NSLocationAlwaysUsageDescription

  키를 등록해야한다 NSLocationWhenInUseUsageDescription은 앱을 실행중에만 사용하겠다 즉 앱이

  active상태일때만 위치정보를 사용한다는 것이다. NSLocationAlwaysUsageDescription은 항상 사용하겠다는 것이다.

 

if(_locationManager == nil){
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager setDelegate:self];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
        if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [self.locationManager requestWhenInUseAuthorization];
        }else{
            [_locationManager startUpdatingLocation];
        }

    }

이렇게하면 위치정보를 사용할수 있게된다.


3. 이제부터는 본격적인 비콘 사용법이다.


@property (nonatomic, strong) CBCentralManager* centralManager;

 

이 manager을 선언한다.


- (void)startBeaconScan

{

    if(_centralManager != nil){

        [self stopBeaconScan];

    }

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];

    [self startLocationMonitoring];

}


- (void)startLocationMonitoring

{

    if(![CLLocationManager locationServicesEnabled]){

    if(_region == nil){

        _region = [[CLBeaconRegion alloc] initWithProximityUUID:

[[NSUUID alloc] initWithUUIDString:BEACON_UUID]

                                                                    identifier:BEACON_IDENTIFIER];

    }

        [_locationManager startMonitoringForRegion:_region];

        [_locationManager startRangingBeaconsInRegion:_region];

}


이렇게하면 비콘을 비콘을 사용할수 있게된다 BEACON_UUID은 비콘을 제작한 회사 uuid인듯하다.


locationManager에 델리게이트 메소드에서 비콘을 탐지할수 있게된다


-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region



더 궁금한내용 있으시면 댓글 달아주세요










 



'iOS' 카테고리의 다른 글

[swift] iOS 주소 찾기  (0) 2017.11.15
앱스토어 버전 가져오기  (0) 2015.09.11
iOS AudioSession 공부중....  (0) 2015.05.28
NSDate 대해서  (0) 2015.05.06
apple mapKit 사용방(구글이 안해주는 길찾기기능 포함)  (0) 2015.04.30