iBeacon iOS8 領域観測サービスPeripheral(発信側)作成

iOS8 iBeacon発信機を作ってみました。
参考にしたのは、「[iOS 7] 新たな領域観測サービス iBeacon を使ってみる」のサンプルをベースにリメイクしています。内容等に関しては、サンプル提供ページを参照願います。
発信ボタンと停止ボタンを設けて更にビーコン的なパルス発信も装備し操作しやすいようにしています。ビーコン的なパルス発信はビーコン的な演出をする for iBeacon」を参考にしました。

 

UUIDを変えれば発信機と使用できます。これで動作確認は容易に出来ると思いますので試してみてください。

iOS8 iBeaconに関する仕様

ライブラリの追加

Beacon による領域観測機能を利用するために必要

CoreLocation.framework

CoreBluetooth.framework

AppDelegate.m

#import "AppDelegate.h"


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    

    // iOS8 ローカル通知を動作させる許可を得る必要

    if ([UIApplication  

       instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {

        [[UIApplication sharedApplication]

         registerUserNotificationSettings:[UIUserNotificationSettings

         settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound

                               categories:nil]];

    }

    

    return YES;

}

 

- (void)applicationWillResignActive:(UIApplication *)application

{


}


- (void)applicationDidEnterBackground:(UIApplication *)application

{


}


- (void)applicationWillEnterForeground:(UIApplication *)application

{


}


- (void)applicationDidBecomeActive:(UIApplication *)application

{


}


- (void)applicationWillTerminate:(UIApplication *)application

{

 

}


@end

ViewController.m

#import "ViewController.h"

#import <CoreBluetooth/CoreBluetooth.h>

#import <CoreLocation/CoreLocation.h>

#import "PulsingHaloLayer.h"


#define kMaxRadius 160


@interface ViewController () <CBPeripheralManagerDelegate> {

    

    int btn;

}


@property (nonatomic) NSUUID *proximityUUID;

@property (nonatomic) CBPeripheralManager *peripheralManager;


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

@property (strong, nonatomic) NSDictionary *myBeaconData;

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


// Beacon

@property (weak, nonatomic) IBOutlet UIImageView *beaconView;


@property (nonatomic, strong) PulsingHaloLayer *halo;


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // ボタン初期値

    btn = 1;

    

    // 背景に画像をセット

    UIImage *image = [UIImage imageNamed:@"bg02"];

    self.view.backgroundColor = [UIColor colorWithPatternImage:image];

    

    // Create a NSUUID object

    // 生成したUUIDから送信NSUUIDオブジェクトを作成します。送受信同じNSUUID

    self.proximityUUID = [[NSUUID alloc] initWithUUIDString:@"生成したUUID"];

    

    self.peripheralManager = [[CBPeripheralManager alloc]

                         initWithDelegate:self

                                    queue:nil

                                  options:nil];


}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


// [発信]ボタンを押した時

- (IBAction)btStat:(id)sender {

    

    // Statボタン2度押し禁止

    if (btn == 1) {

        

        //////////////////////////////////////////////////////////////////

        // Initialize the Beacon Region

        // ビーコン領域を初期化します Region:領域、範囲

        CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]

                                        initWithProximityUUID:self.proximityUUID

                                                                   major:1

                                                                   minor:2

                                                                            

                                        identifier:@"jp.classmethod.testregion"];

        // Get the beacon data to advertise

        // 発信、公開

        // ビーコンデータを広告することを得る peripheral:周辺 With:共に Measured:測定された

        // peripheralDataWithMeasuredPower: ペリフェラルの 1m 地点での電波強度を表すNSNumber

        NSDictionary *beaconPeripheralData = [beaconRegion

                                            peripheralDataWithMeasuredPower:nil];

        [self.peripheralManager startAdvertising:beaconPeripheralData];

        

        ///////////////////////////////////////////////////////////////////

        

        // パルス波形表示設定

        [self puls:1];

        

        self.statusLabel.text = @"発信中";

        

        // UUIDの表示

        NSString *lbuuid = beaconRegion.proximityUUID.UUIDString;

        self.lbUUID.text = [NSString stringWithFormat:@"UUID: %@", lbuuid];

        

        btn = 0;

    }

}


// [停止]ボタンを押した時

- (IBAction)btStop:(id)sender {

    

    if (btn == 0) {

        // パルス波形表示設定

        [self puls:0];

        

        self.lbUUID.text = nil;

        

        // BLEアドバタイズ停止処理

        [self.peripheralManager stopAdvertising];

        

        btn = 1;

    }

}


- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error

{

    if (error) {

        [self sendLocalNotificationForMessage:[NSString stringWithFormat:@"%@", error]];

    } else {

        [self sendLocalNotificationForMessage:@"アドバタイズ(発信)スタート"];

    }

}


- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral

{

    NSString *message;

    

    switch (peripheral.state) {

        case CBPeripheralManagerStatePoweredOff:

            message = @"電源OFF";

            break;

        case CBPeripheralManagerStatePoweredOn:

            message = @"電源ON";

            //[self startAdvertising];

            break;

        case CBPeripheralManagerStateResetting:

            message = @"リセット";

            break;

        case CBPeripheralManagerStateUnauthorized:

            message = @"許可されていません";

            break;

        case CBPeripheralManagerStateUnknown:

            message = @"不明";

            break;

        case CBPeripheralManagerStateUnsupported:

            message = @"サポート対象外";

            break;

            

        default:

            break;

    }

    

    [self sendLocalNotificationForMessage:[@"ペリフェラル(アドバタイズ・発信公開)を行いました: " stringByAppendingString:message]];

}


#pragma mark - Private methods



// iBeaconパルス波形表示設定 引数1つ半径 arg01(ON:1 OFF:0)

- (void)puls:(int)arg01 {

    

    // arg01(ON:1 OFF:0)

    if (arg01 == 1) {

        

        // プロパティに所望の値をセットすれば、半径が変わります。

        self.halo = [PulsingHaloLayer layer];

        

        self.beaconView.hidden = NO; //表示にする

        // Beacon

        self.halo.position = self.beaconView.center;

        

        [self.view.layer insertSublayer:self.halo below:self.beaconView.layer];

        

        self.halo.radius = 1.0;

        

        // Beacon ON:1

        self.halo.radius = 1.0 * kMaxRadius;

        

        // 色を変える

        UIColor *color = [UIColor colorWithRed:0.6    // 0

                                         green:0.0    // 0.487

                                          blue:0.5    // 1.0

                                         alpha:1.0];

        

        self.halo.backgroundColor = color.CGColor;

        

    } else {

        

        //self.beaconView.hidden = YES; //非表示にする

        self.halo.backgroundColor = nil; //非表示にする

        self.statusLabel.text = @"停止中";

        self.statusLabel.textColor = [UIColor redColor];

    }

    

}


- (void)sendLocalNotificationForMessage:(NSString *)message

{

    UILocalNotification *localNotification = [UILocalNotification new];

    localNotification.alertBody = message;

    localNotification.fireDate = [NSDate date];

    localNotification.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}


@end

▫️参考にしたページ

▫️スライド

 

目 次