iBeaconsを触ってみた


% uuidgen
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
// UUIDを適当に作成(uuidgenコマンドで生成する)
        NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
        
        // アドバタイズ用のデータを作成
        MBCBeaconAdvertisementData *beaconData
        = [[MBCBeaconAdvertisementData alloc] initWithProximityUUID:proximityUUID
                                                              major:1
                                                              minor:1
                                                      measuredPower:-58];
        
        // アドバタイズ開始
        [peripheral startAdvertising:beaconData.beaconAdvertisement];

//----------------------------------------------------
// ViewController.h
//----------------------------------------------------

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

@interface ViewController : UIViewController <CBPeripheralManagerDelegate>
@end

//----------------------------------------------------
// ViewController.m
//----------------------------------------------------

- (void)viewDidLoad
{
    [super viewDidLoad];
    // CBPeripheralManagerを作成
    self.manager    = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];
    
    // アドバタイズを開始
    [self beginAdvertising];
}

- (void)beginAdvertising
{
    NSUUID      *uuid  = [[NSUUID alloc]initWithUUIDString:self.uuidField.text];
    uint16_t    major  = (uint16_t)[self.majorField.text integerValue];
    uint16_t    manor  = (uint16_t)[self.minorField.text integerValue];
    
    // uuid, major, minor, identifierを指定して BeconRegionを作成
    CLBeaconRegion *beacon      = [[CLBeaconRegion alloc]initWithProximityUUID:uuid
                                                                         major:major
                                                                         minor:manor
                                                                    identifier:self.identifierField.text];
    
    NSDictionary *beaconData    = [beacon peripheralDataWithMeasuredPower:nil];
    
    [self.manager stopAdvertising];
    [self.manager startAdvertising:beaconData];
}

//----------------------------------------------------------------
// Required Method ペリフェラルデバイスの状態を監視
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
    switch (peripheral.state) {
        case CBPeripheralManagerStatePoweredOn:
            self.statusLabel.text = @"CBPeripheralManagerStatePoweredOn";
            break;
        case CBPeripheralManagerStatePoweredOff:
            self.statusLabel.text = @"CBPeripheralManagerStatePoweredOff";
            break;
        case CBPeripheralManagerStateResetting:
            self.statusLabel.text = @"CBPeripheralManagerStateResetting";
            break;
        case CBPeripheralManagerStateUnauthorized:
            self.statusLabel.text = @"CBPeripheralManagerStateUnauthorized";
            break;
        case CBPeripheralManagerStateUnknown:
            self.statusLabel.text = @"CBPeripheralManagerStateUnknown";
            break;
        case CBPeripheralManagerStateUnsupported:
            self.statusLabel.text = @"CBPeripheralManagerStateUnsupported";
            break;
    }
}

//----------------------------------------------------
// ViewController.h
//----------------------------------------------------

#import <UIKit/UIKit.h>
@import CoreLocation;

@interface ViewController : UIViewController <CLLocationManagerDelegate>
@end

//----------------------------------------------------
// ViewController.m
//----------------------------------------------------

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    if ( [CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]] ) {
        
        // Managerを作成
        self.manager            = [CLLocationManager new];
        self.manager.delegate   = self; // デリゲートを設定
        
        // Beacon情報を設定(ここで設定した情報のビーコンを監視します)
        self.proximityUUID      = [[NSUUID alloc]initWithUUIDString:UUID];
        self.identifier         = IDENTIFIER;
        self.major              = (uint16_t)[MAJOR integerValue];
        self.minor              = (uint16_t)[MINOR integerValue];
        
        // Beacon情報を元にリージョンを作成
        self.region = [[CLBeaconRegion alloc]initWithProximityUUID:self.proximityUUID
                                                             major:self.major
                                                             minor:self.minor
                                                        identifier:self.identifier];
        self.region.notifyOnEntry               = YES; // 領域に入った事を監視
        self.region.notifyOnExit                = YES; // 領域を出た事を監視
        self.region.notifyEntryStateOnDisplay   = NO; // デバイスのディスプレイがオンのとき、ビーコン通知が送信されないように設定
        
        [self.manager startMonitoringForRegion:self.region]; // 領域監視を開始
        [self.manager startRangingBeaconsInRegion:self.region]; // iBeaconとの距離測定を開始
    }
}

#pragma mark CLLocationManagerDelegate

//-------------------------------------
// 領域に入った時
- (void)locationManager:(CLLocationManager *)manager
         didEnterRegion:(CLRegion *)region
{
    [self sendNotification:@"ようこそ!"];
}

//-------------------------------------
// 領域から出た時
- (void)locationManager:(CLLocationManager *)manager
          didExitRegion:(CLRegion *)region
{
    [self sendNotification:@"さようなら!"];
}

//--------------------------------------------------------------
// iBeaconを監視
- (void)locationManager:(CLLocationManager *)manager
        didRangeBeacons:(NSArray *)beacons
               inRegion:(CLBeaconRegion *)region
{
    // init
    NSString *uuid                          = @"unknown";
    CLProximity proximity                   = CLProximityUnknown;
    CLLocationAccuracy accuracy             = 0.0;
    NSInteger rssi                          = 0;
    NSNumber *major                         = @0;
    NSNumber *minor                         = @0;
    
    // near beacon
    CLBeacon *beacon    = beacons.firstObject;
    
    uuid                = beacon.proximityUUID.UUIDString;
    proximity           = beacon.proximity;
    accuracy            = beacon.accuracy;
    rssi                = beacon.rssi;
    major               = beacon.major;
    minor               = beacon.minor;
    
    // update view
    self.uuidLabel.text         = beacon.proximityUUID.UUIDString;
    self.majorLabel.text        = [NSString stringWithFormat:@"%@", major];
    self.minorLabel.text        = [NSString stringWithFormat:@"%@", minor];
    self.accuracyLabel.text = [NSString stringWithFormat:@"%f", accuracy];
    self.rssiLabel.text = [NSString stringWithFormat:@"%ld", (long)rssi];
    
    switch (proximity) {
        case CLProximityUnknown:
            self.proximityLabel.text    = @"CLProximityUnknown";
            break;
        case CLProximityImmediate:
            self.proximityLabel.text    = @"CLProximityImmediate";
            break;
        case CLProximityNear:
            self.proximityLabel.text    = @"CLProximityNear";
            break;
        case CLProximityFar:
            self.proximityLabel.text    = @"CLProximityFar";
            break;
        default:
            break;
    }
    
    if ( proximity == CLProximityUnknown ) {
        self.beconStateLabel.text   = @"UNKNOWN";
    } else {
        self.beconStateLabel.text   = @"ENTER";
    }
    
    //-------------------------------------------------------------
    // iBeaconの電波強度を調べて、近距離に来た場合 
    if ( proximity == CLProximityImmediate && rssi > -40 ) {
        self.beconStateLabel.text   = @"TOUCH";
    }
}


最新記事

カテゴリー

ハッシュタグ