iBeacon
iBeacon介绍
iBeacon是苹果在2013年WWDC上推出一项基于蓝牙4.0(Bluetooth LE | BLE | Bluetooth Smart)的精准微定位技术,当你的手持设备靠近一个Beacon基站时,设备就能够感应到Beacon信号,范围可以从几毫米到50米。因为是一种定位技术,苹果将iBeacon相关的接口放到了CoreLocation.framework。
使用iBeacon的前提条件,是用户开启支持BLE的iOS终端的蓝牙功能,并开启位置信息服务。iOS在接收到iBeacon后,会将UUID告知使用iBeacon的App。在UUID的触发下,App就会被叫醒。
本文转自Migrant的博客,原文:《Can you Smell the iBeacon?》
- @implementation ViewController
- {
- CBPeripheralManager *_peripheralManager;
- BOOL _isAdvertising;
- }
- – (void)_startAdvertising
- {
- NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:@“B9407F30-F5F8-466E-AFF9-25556B57FE6D”];
- CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID
- major:2
- minor:1
- identifier:@“SimEstimote”];
- NSDictionary *beaconPeripheralData = [region peripheralDataWithMeasuredPower:nil];
- [_peripheralManager startAdvertising:beaconPeripheralData];
- }
- – (void)_updateEmitterForDesiredState
- {
- if (_peripheralManager.state == CBPeripheralManagerStatePoweredOn)
- {
- // only issue commands when powered on
- if (_isAdvertising)
- {
- if (!_peripheralManager.isAdvertising)
- {
- [self _startAdvertising];
- }
- }
- else
- {
- if (_peripheralManager.isAdvertising)
- {
- [_peripheralManager stopAdvertising];
- }
- }
- }
- }
- #pragma mark – CBPeripheralManagerDelegate
- – (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
- {
- [self _updateEmitterForDesiredState];
- }
- #pragma mark – Actions
- – (IBAction)advertisingSwitch:(UISwitch *)sender
- {
- _isAdvertising = sender.isOn;
- [self _updateEmitterForDesiredState];
- }
- @end
- B9407F30-F5F8-466E-AFF9-25556B57FE6D
- @implementation AppDelegate
- {
- CLLocationManager *_locationManager;
- BOOL _isInsideRegion; // flag to prevent duplicate sending of notification
- }
- – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options
- {
- // create a location manager
- _locationManager = [[CLLocationManager alloc] init];
- // set delegate, not the angle brackets
- _locationManager.delegate = self;
- NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:@“B9407F30-F5F8-466E-AFF9-25556B57FE6D”];
- CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID
- identifier:@“Estimote Range”];
- // launch app when display is turned on and inside region
- region.notifyEntryStateOnDisplay = YES;
- // isMonitoringAvailableForClass:Returns a Boolean indicating whether the device supports region monitoring using the specified class.
- if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
- {
- [_locationManager startMonitoringForRegion:region];
- // get status update right away for UI
- [_locationManager requestStateForRegion:region];
- }
- else
- {
- NSLog(@“This device does not support monitoring beacon regions”);
- }
- // Override point for customization after application launch.
- return YES;
- }
- – (void)_sendEnterLocalNotification
- {
- if (!_isInsideRegion)
- {
- UILocalNotification *notice = [[UILocalNotification alloc] init];
- notice.alertBody = @“Inside Estimote beacon region!”;
- notice.alertAction = @“Open”;
- [[UIApplication sharedApplication] scheduleLocalNotification:notice];
- }
- _isInsideRegion = YES;
- }
- – (void)_sendExitLocalNotification
- {
- if (_isInsideRegion)
- {
- UILocalNotification *notice = [[UILocalNotification alloc] init];
- notice.alertBody = @“Left Estimote beacon region!”;
- notice.alertAction = @“Open”;
- [[UIApplication sharedApplication] scheduleLocalNotification:notice];
- }
- _isInsideRegion = NO;
- }
- – (void)_updateUIForState:(CLRegionState)state
- {
- ViewController *vc = (ViewController *)self.window.rootViewController;
- if (state == CLRegionStateInside)
- {
- vc.label.text = @“Inside”;
- }
- else if (state == CLRegionStateOutside)
- {
- vc.label.text = @“Outside”;
- }
- else
- {
- vc.label.text = @“Unknown”;
- }
- }
- #pragma mark – CLLocationManagerDelegate
- – (void)locationManager:(CLLocationManager *)manager
- didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
- {
- // always update UI
- [self _updateUIForState:state];
- if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
- {
- // don\’t send any notifications
- return;
- }
- if (state == CLRegionStateInside)
- {
- [self _sendEnterLocalNotification];
- }
- else
- {
- [self _sendExitLocalNotification];
- }
- }
- @end
注意: didDetermineState: 在穿越区域边界时和 requestStateForRegion: 方法后都会被调用。所以你可以在这里做关于监听的工作并实现 didEnterRegion 和 didExitRegion: 来保证不会过分干扰用户。 |
本文摘自:http://www.cocoachina.com/applenews/devnews/2014/0515/8427.html
参考资料
http://blog.csdn.net/meway/article/details/25311753
iBeacon开发:
http://esoftmobile.com/2013/12/15/ibeacons/
http://blog.csdn.net/zengraoli/article/details/19614443
iBeacon是什么 iBeacon怎么用:
http://jingyan.baidu.com/article/4d58d541cb12a59dd4e9c0ef.html
如何使用iOS 7的iBeacons来提高你的应用:
http://www.cocoachina.com/applenews/devnews/2014/0115/7705.html
iOS7的iBeacon技术解析
:http://www.w3c.com.cn/ios7%E7%9A%84ibeacon%E6%8A%80%E6%9C%AF%E8%A7%A3%E6%9E%90
自己动手 IBeacon 室内定位 完整体验(超详细过程):
http://blog.csdn.net/u011341435/article/details/18948137
iOS蓝牙调用的一般流程 – ctaodrea:
http://www.tuicool.com/articles/qYv2Mn
iOS CoreBluetooth 教程:
http://zhuhaibobb.blog.163.com/blog/static/2744006720136954416112/
iBeacons开发示例代码:
http://wiki.viewc.com/ibeacons-example-code-cn.html
苹果iOS Developer Library更新(示例代码):
http://ios.9tech.cn/news/2014/0117/39661.html