【问题标题】:beacon array value is empty in didRangeBeacons in objective C目标 C 中的 didRangeBeacons 中的信标数组值为空
【发布时间】:2016-03-30 13:48:44
【问题描述】:

在我的应用程序中,我正在寻找信标并想要计算距离,这两天我遇到了一个问题,即 CCLocationManager 委托函数返回空信标数组。我尝试了 CBCentralManagerDelegate 函数,我得到了信标列表。在 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。

我有一个真正的信标和 ipad 迷你设备。

此代码在所有信标查找示例中都给出。帮助我在这里做错了什么。

【问题讨论】:

  • 如果它们是“真正的 iBeacons”,您应该无法通过 CBCentralManager 看到它们。
  • 如果我是你,我会看这个:raywenderlich.com/101891/ibeacons-tutorial-ios-swift。还要考虑更详细地命名变量。在命名和一般不良的命名约定(deviNamArr)上偷工减料确实令人不悦。 deviNamArr = deviceNames 或 deviceNamesArray.
  • 我正在使用 CBCentralManager 获取此操作:外围设备列表 2016 -03-30 19:33:22.371 BluetoothExample[21355:3454631] 广告数据列表 { kCBAdvDataIsConnectable = 1; kCBAdvDataLocalName = "XY-1D54-64"; kCBAdvDataServiceUUIDs = ("ABC24206-9BD7-034F-4FC9-ED1023171D54"); } -70
  • 好的,谢谢你,我更正了我的变量命名@TheCodingArt

标签: objective-c cllocationmanager ibeacon


【解决方案1】:

几点:

  • 如果您没有收到测距回调,则 ProximityUUID 可能不正确。使用现成的信标检测器,如 Locate,并验证您可以检测到信标,并且 ProximityUUID 是 CLBeaconRegion 设置为检测的内容。

  • 通过转到设置 ->(您的应用名称)验证您已正确获得位置权限,并验证位置设置为“始终”

  • 检测 iBeacon 时无需使用 CoreBluetooth。您应该删除所有带有 CB 前缀的类的行。虽然此代码将为您提供附近的 iBeacon 蓝牙设备列表,但它不会让您读取信标标识符。您必须使用 CoreLocation 来执行此操作。

  • 此方法调用由 CoreLocation 框架进行,不应手动调用。它应该被删除: [self locationManager:locManagerdidStartMonitoringForRegion:clBeconRegion];

【讨论】:

  • 我已经按照你所说的验证了 ProximityUUID。位置始终只在其中。 CoreBluetooth 是我已经检查了我使用的 UUID 和信标名称,然后我只尝试了 CoreLocation 和它们的委托函数,它也只给出空数组。最后我也评论了调用 didStartMonitoringForRegion。
  • 注意您从 CoreLocation 获得的蓝牙设备 UUID。 这与用于检测信标的 ProximityUUID 不同。
  • 我怀疑代码中使用的 ProximityUUID 不是信标传输的那个。我会再检查一次,这次不使用 CoreBluetooth,而是使用 Locate 应用程序:itunes.apple.com/us/app/locate-beacon/id738709014?mt=8
猜你喜欢
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 2010-12-13
  • 1970-01-01
  • 2012-01-15
相关资源
最近更新 更多