Last active
December 31, 2015 07:48
-
-
Save t-kashima/7955998 to your computer and use it in GitHub Desktop.
iBeaconのセントル側のサンプルコード
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
// | |
// ViewController.h | |
// iBeaconTester | |
// | |
// Created by Kashima Takumi on 2013/12/14. | |
// Copyright (c) 2013年 UNUUU FOUNDATION. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@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
// | |
// ViewController.m | |
// iBeaconTester | |
// | |
// Created by Kashima Takumi on 2013/12/14. | |
// Copyright (c) 2013年 UNUUU FOUNDATION. All rights reserved. | |
// | |
#import "ViewController.h" | |
#import <CoreLocation/CoreLocation.h> | |
#define ROOMID 14852875 | |
#define ACCOUNTID 652829 | |
@interface ViewController () <CLLocationManagerDelegate> | |
@property (nonatomic) CLLocationManager *locationManager; | |
@property (nonatomic) NSUUID *proximityUUID; | |
@property (nonatomic) CLBeaconRegion *beaconRegion; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) { | |
self.locationManager = [CLLocationManager new]; | |
self.locationManager.delegate = self; | |
// 観測点のUUIDを生成 | |
self.proximityUUID = [[NSUUID alloc] initWithUUIDString:@"42EAC82B-80B0-427B-922B-26D32033C356"]; | |
// UUIDとRegionIDを渡すことで観測点を一意に特定する | |
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.proximityUUID identifier:@"com.unuuu.ibeacon_1"]; | |
// Beaconによる領域観測を開始 | |
[self.locationManager startMonitoringForRegion:self.beaconRegion]; | |
} | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - CLLocationManagerDelegate methods | |
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region | |
{ | |
[self sendLocalNotificationForMessage:@"Start Monitoring Region"]; | |
// [self.locationManager requestStateForRegion:self.beaconRegion]; | |
} | |
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region | |
{ | |
switch (state) { | |
// 既にリージョンの中にいる時 | |
case CLRegionStateInside: | |
if ([region isMemberOfClass:[CLBeaconRegion class]] && [CLLocationManager isRangingAvailable]) { | |
[self.locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
// [self locationManager:manager didEnterRegion:region]; | |
} | |
break; | |
case CLRegionStateOutside: | |
case CLRegionStateUnknown: | |
default: | |
break; | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region | |
{ | |
[self sendLocalNotificationForMessage:@"Enter Region"]; | |
if ([region isMemberOfClass:[CLBeaconRegion class]] && [CLLocationManager isRangingAvailable]) { | |
[self.locationManager startRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region | |
{ | |
[self sendLocalNotificationForMessage:@"Exit Region"]; | |
if ([region isMemberOfClass:[CLBeaconRegion class]] && [CLLocationManager isRangingAvailable]) { | |
[self.locationManager stopRangingBeaconsInRegion:(CLBeaconRegion *)region]; | |
} | |
// モニタリングが終了しているので再度モニタリングを開始する | |
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) { | |
// Beaconによる領域観測を開始 | |
[self.locationManager startMonitoringForRegion:self.beaconRegion]; | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region | |
{ | |
if (beacons.count > 0) { | |
CLBeacon *nearestBeacon = beacons.firstObject; | |
NSString *rangeMessage; | |
switch (nearestBeacon.proximity) { | |
case CLProximityImmediate: | |
rangeMessage = @"Range Immediate: "; | |
break; | |
case CLProximityNear: | |
rangeMessage = @"Range Near: "; | |
break; | |
case CLProximityFar: | |
rangeMessage = @"Range Far: "; | |
break; | |
default: | |
rangeMessage = @"Range Unknown: "; | |
break; | |
} | |
if (nearestBeacon.proximity == CLProximityUnknown) { | |
[self locationManager:manager didExitRegion:region]; | |
return; | |
} | |
// NSString *message = [NSString stringWithFormat:@"major:%@, minor:%@, accuracy:%f, rssi:%d", | |
// nearestBeacon.major, nearestBeacon.minor, nearestBeacon.accuracy, nearestBeacon.rssi]; | |
// [self sendLocalNotificationForMessage:[rangeMessage stringByAppendingString:message]]; | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error | |
{ | |
[self sendLocalNotificationForMessage:@"Exit Region"]; | |
} | |
#pragma mark - Private methods | |
- (void)sendLocalNotificationForMessage:(NSString *)message | |
{ | |
UILocalNotification *localNotification = [UILocalNotification new]; | |
localNotification.alertBody = message; | |
localNotification.fireDate = [NSDate date]; | |
localNotification.soundName = UILocalNotificationDefaultSoundName; | |
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment