Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created December 14, 2013 05:31
Show Gist options
  • Save t-kashima/7955995 to your computer and use it in GitHub Desktop.
Save t-kashima/7955995 to your computer and use it in GitHub Desktop.
iBeaconのペリフェラル側のサンプルコード
//
// ViewController.h
// iBeaconPeripheralTester
//
// Created by Kashima Takumi on 2013/12/14.
// Copyright (c) 2013年 UNUUU FOUNDATION. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// iBeaconPeripheralTester
//
// Created by Kashima Takumi on 2013/12/14.
// Copyright (c) 2013年 UNUUU FOUNDATION. All rights reserved.
//
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CBPeripheralManagerDelegate>
@property (nonatomic) NSUUID *proximityUUID;
@property (nonatomic) CBPeripheralManager *peripheralManager;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.proximityUUID = [[NSUUID alloc] initWithUUIDString:@"42EAC82B-80B0-427B-922B-26D32033C356"];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
if (self.peripheralManager.state == CBPeripheralManagerStatePoweredOn) {
[self startAdvertising];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
{
if (error) {
[self sendLocalNotificationForMessage:[NSString stringWithFormat:@"%@", error]];
} else {
[self sendLocalNotificationForMessage:@"Start Advertising"];
}
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
NSString *message;
switch (peripheral.state) {
case CBPeripheralManagerStatePoweredOff:
message = @"PoweredOff";
break;
case CBPeripheralManagerStatePoweredOn:
message = @"PoweredOn";
[self startAdvertising];
break;
case CBPeripheralManagerStateResetting:
message = @"Resetting";
break;
case CBPeripheralManagerStateUnauthorized:
message = @"Unauthorized";
break;
case CBPeripheralManagerStateUnknown:
message = @"Unknown";
break;
case CBPeripheralManagerStateUnsupported:
message = @"Unsupported";
break;
default:
break;
}
[self sendLocalNotificationForMessage:[@"PeripheralManager did update state: " stringByAppendingString:message]];
}
#pragma mark - Private methods
- (void)startAdvertising
{
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.proximityUUID
major:1
minor:2
identifier:@"com.unuuu.ibeacon_1"];
NSDictionary *beaconPeripheralData = [beaconRegion peripheralDataWithMeasuredPower:nil];
[self.peripheralManager startAdvertising:beaconPeripheralData];
}
- (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