iBeacon技术不多赘述,本文重点关注如何在iOS上扫描周围的iBeacon信号并读取信息,包括beacon设备的UUID,Major,Minor和当前距离。
0. 添加location权限
找到project目录下Supporting Files -> Info.plist -> 右键空白"Add Row" -> 输入“NSLocationWhenInUseUsageDescription”,value为提示信息,可以为空。
1. 添加CoreLocation.framework
Build Phases -> Link Binary With Libraries -> add "CoreLocation.framework"
2. 引入库CoreLocation/CoreLocation.h
#import <CoreLocation/CoreLocation.h>
3. 添加CLLocationManagerDelegate
@interface BeaconViewController : UITableViewController <CLLocationManagerDelegate>
4. 声明对象
@interface BeaconViewController : UITableViewController <CLLocationManagerDelegate>{
CLLocationManager *_locationManager;<span style="white-space:pre"> </span>//
CLBeaconRegion *_region;<span style="white-space:pre"> </span>//
NSArray *_detectedBeacons;<span style="white-space:pre"> </span>//存放接收到的beacons
}
5. 初始化对象
#pragma mark - Init Beacons
- (void) initLocationManager{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self checkLocationAccessForRanging];
}
}
- (void) initDetectedBeaconsList{
if (!_detectedBeac