iBeacon基站可以创建一个信号区域,当进入和离开该区域时,应用就会检测到并发出通知。iBeacon类似一个热点,只要进入它的信号区域,就可以获取它发出的信息,而无需先连接上。
iBeacon是基于蓝牙和定位的,如同蓝牙广播一样发送信息,只有进入信号区域才能收到,所以iBeacon并没放在CoreBluetooth中,而是在位置相关的CoreLocation里。但是使用时要开启蓝牙
1.首先要在工程里的info.plist增加字段NSLocationAlwaysUsageDescription(这个是允许一直在后台运行的)
2 .h文件
#import<UIKit/UIKit.h>#import<CoreLocation/CoreLocation.h>@interface ViewController : UIViewController<CLLocationManagerDelegate,>@PRoperty (strong, nonatomic) CLBeaconRegion *beacon1;//被扫描的iBeacon@property (strong, nonatomic) CLLocationManager * locationmanager;@end,,,3 .m文件
#define BEACONUUID @"12334566-7173-4889-9579-954995439125"//iBeacon的uuid可以换成自己设备的uuid- (void)viewDidLoad {[super viewDidLoad];self.locationmanager = [[CLLocationManager alloc] init];//初始化self.locationmanager.delegate = self;self.beacon1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:BEACONUUID] identifier:@"media"];//初始化监测的iBeacon信息[self.locationmanager requestAlwaysAuthorization];//设置location是一直允许}- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{if (status == kCLAuthorizationStatusAuthorizedAlways) {[self.locationmanager startMonitoringForRegion:self.beacon1];//开始MonitoringiBeacon}}//发现有iBeacon进入监测范围-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{[self.locationmanager startRangingBeaconsInRegion:self.beacon1];//开始RegionBeacons}//找的iBeacon后扫描它的信息- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{//如果存在不是我们要监测的iBeacon那就停止扫描他if (![[region.proximityUUID UUIDString] isEqualToString:BEACONUUID]){[self.locationmanager stopMonitoringForRegion:region];[self.locationmanager stopRangingBeaconsInRegion:region];}//打印所有iBeacon的信息for (CLBeacon* beacon in beacons) {NSLog(@"rssi is :%ld",beacon.rssi);NSLog(@"beacon.proximity %ld",beacon.proximity);......}}uuid唯一标识此类iBeacon。
proximity远近范围的,有Near(在几米内),Immediate(在几厘米内),Far(超过 10 米以外,不过在测试中超不过10米就是far),Unknown(无效)
major和minor组合后区分同一类型下的iBeacon。
accuracy和iBeacon的距离
rssi信号轻度为负值,越接近0信号越强,等于0时无法获取信号强度
关于iBeacon的10个真相
如何接收iBeacon信号并读取其信息
新闻热点
疑难解答