在我的应用程序中我找到了信标&想要计算距离,这里两天我遇到了CCLocationManager委托函数中的一个问题返回空信标array.i尝试了CBCentralManagerDelegate函数我得到beacons的列表。在didRangeBeacons方法中给出空数组帮助我。下面是我试过的代码
代码:
@interface ViewController ()<CBCentralManagerDelegate,CLLocationManagerDelegate>
{
NSMutableData *dat;
CLBeaconRegion *clBeconRegion;
CLLocationManager *locManager;
}
@property (strong,nonatomic) CBCentralManager *cbcManager;
- (IBAction)scanBtn:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.cbcManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil];
locManager=[[CLLocationManager alloc] init];
locManager.delegate=self;
[self initRegion];
clBeconRegion.notifyEntryStateOnDisplay=YES;
[locManager requestAlwaysAuthorization];
[self locationManager:locManager didStartMonitoringForRegion:clBeconRegion];
}
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"List of peripheral %@",peripheral);
NSLog(@"List of advertisementData %@ %@",advertisementData,RSSI);
}
-(void)initRegion{
NSUUID *uuid=[[NSUUID alloc]initWithUUIDString:@"XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
clBeconRegion=[[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:@"BluetoothExample"];
clBeconRegion.notifyEntryStateOnDisplay=YES;
clBeconRegion.notifyOnEntry=YES;
clBeconRegion.notifyOnExit=YES;
[locManager startMonitoringForRegion:clBeconRegion];
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"didConnectPeripheral");
[peripheral discoverServices:nil];
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
[locManager startRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@"Entered Region");
[locManager startRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@"Exit Region");
[locManager stopRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
NSLog(@"beacons %@ last %@",beacons,[region proximityUUID]);
CLBeacon *beacon=[[CLBeacon alloc]init];
beacon=[beacons lastObject];
NSLog(@"min %@ maj %@",beacon.minor,beacon.major);
}
- (IBAction)scanBtn:(id)sender
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.cbcManager scanForPeripheralsWithServices:nil options:options];
NSLog(@"scan clicked");
}
我修复了NSLocationAlwaysUsageDescription info.plist。
我有一个真正的灯塔&amp; ipad mini设备。
这个代码在所有信标查找示例中给出。帮助我在这里犯了什么错误。
答案 0 :(得分:0)
几点:
如果您没有获得测距回调,ProximityUUID可能不正确。使用像Locate这样的现成信标检测器,并验证您是否可以检测到信标,并且ProximityUUID是CLBeaconRegion设置为检测的内容。
转到设置 - &gt;验证您是否已正确获取位置权限(您的应用名称),并且验证位置设置为&#34;始终&#34;
检测iBeacons时无需使用CoreBluetooth。您应该删除具有CB前缀的类的所有行。虽然此代码将为您提供附近的iBeacon蓝牙设备列表,但它不会让您读取信标标识符。您必须使用CoreLocation来执行此操作。
此方法调用由CoreLocation框架进行,不应手动调用。应将其删除:
[self locationManager:locManagerdidStartMonitoringForRegion:clBeconRegion];