//引入框架 Info.plist需添加定位权限
#import<CoreLocation/CoreLocation.h>//CLLocationManagerDelegate
//声明赛格属性
@property (nonatomic, strong) NSArray *beaconArr;//存放扫描到的iBeacon
@property (strong, nonatomic) CLBeaconRegion *beacon1;//被扫描的iBeacon
@property (strong, nonatomic) CLLocationManager *locationManager;
///检测蓝牙状态需要引入框架
#import <CoreBluetooth/CoreBluetooth.h>//CBCentralManagerDelegate
初始化数据
- (void)viewDidLoad {
[super viewDidLoad];
//检测蓝牙状态
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
//检测beacon设备
self.searchArray = [NSMutableArray array];
self.beaconArr = [NSArray array];
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];//初始化
self.locationManager.delegate = self;
// self.locationManager.activityType = CLActivityTypeFitness;
// self.locationManager.distanceFilter = kCLDistanceFilterNone;
//每隔多少米定位一次(这里的设置为任何的移动)
// _locationManager.distanceFilter=kCLDistanceFilterNone;
// _locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
//这句话ios8以上版本使用。
[self.locationManager requestAlwaysAuthorization];//设置location是一直允许
self.beacon1 = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:BEACONUUID] identifier:BEACONUUID];//初始化监测的iBeacon信息
self.beacon1.notifyEntryStateOnDisplay = YES;
// [self.locationManager startMonitoringForRegion:self.beacon1];
[self.locationManager startRangingBeaconsInRegion:self.beacon1];
}else{
NSLog(@"提醒用户打开定位权限");
}
// if ([CLLocationManager locationServicesEnabled]) {
// NSLog(@"定位功能可用");
// //定位功能可用
//
// }else
if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
//定位不能用
NSLog(@"拒绝获取定位");
[[HudManager manager] showHudWithText:@"用户拒绝了获取定位"];
}
if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusRestricted) {
//定位不能用
NSLog(@"应用程序的定位权限被限制");
}
}
检测蓝牙状态
#pragma mark -- CBCentralManagerDelegate
-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
/*
CBCentralManagerStateUnknown = CBManagerStateUnknown,
CBCentralManagerStateResetting = CBManagerStateResetting,
CBCentralManagerStateUnsupported = CBManagerStateUnsupported,
CBCentralManagerStateUnauthorized = CBManagerStateUnauthorized,
CBCentralManagerStatePoweredOff = CBManagerStatePoweredOff,
CBCentralManagerStatePoweredOn = CBManagerStatePoweredOn,
*/
switch (central.state) {
case CBCentralManagerStateUnknown:
NSLog(@"蓝牙状态未知");//0
break;
case CBCentralManagerStateResetting:
NSLog(@"设备连接蓝牙失败,请重试");//1
break;
case CBCentralManagerStateUnsupported:
NSLog(@"当前设备不支持蓝牙");//2
break;
case CBCentralManagerStateUnauthorized:
NSLog(@"当前设备未授权使用蓝牙");//3
break;
case CBCentralManagerStatePoweredOff:{
NSLog(@"蓝牙没有开启,在设置中打开蓝牙");//4
}
break;
case CBCentralManagerStatePoweredOn:
NSLog(@"蓝牙已成功开启");//5
break;
default:
break;
}
}
遵守扫描IBeacon需要的协议方法
#pragma mark -- CLLocationManagerDelegate
//发现有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{
// NSLog(@"注册的Beacon信息===%@", region);
//如果存在不是我们要监测的iBeacon那就停止扫描他
if (![[region.proximityUUID UUIDString] isEqualToString:BEACONUUID]){
[self.locationManager stopMonitoringForRegion:region];
[self.locationManager stopRangingBeaconsInRegion:region];
NSLog(@"停止扫描Beacon");
}
//控制是否实时刷新
self.beaconArr = beacons;
}
打印详情数据
//将beacon的信息转换为NSString并返回
- (NSString *)detailsStringForBeacon:(CLBeacon *)beacon
{
NSString *format = @"uuid=%@ \n major=%@ • minor=%@ \n proximity=%@ \n accuracy=%f • rssi=%li \n 实际距离=%f米";
return [NSString stringWithFormat:format, beacon.proximityUUID, beacon.major, beacon.minor, [self stringForProximity:beacon.proximity], beacon.accuracy, beacon.rssi,[self calcDistByRSSI:beacon.rssi]];
}
#pragma mark -- 计算距离公式
- (float)calcDistByRSSI:(NSInteger)rssi
{
int i = (int)rssi;
int iRssi = abs(i);
float power = (iRssi-59)/(10*2.0);
return pow(10, power);
}
- (NSString *)stringForProximity:(CLProximity)proximity{
NSString *proximityValue;
switch (proximity) {
case CLProximityNear:
proximityValue = @"Near近";//近 (一米以内)
break;
case CLProximityImmediate:
proximityValue = @"Immediate超近";//超 近 (十厘米以内)
break;
case CLProximityFar:
proximityValue = @"Far远";//远 (一米以外)
break;
case CLProximityUnknown:
default:
proximityValue = @"Unknown未知状态";//不见了 (未知状态)
break;
}
return proximityValue;
}
网友评论