是否可以使用目标C扫描具有位置服务禁用功能的信标?

时间:2016-05-24 11:06:09

标签: ios bluetooth location-services beacon

如果蓝牙和位置服务已开启,我可以成功扫描信标。但我想知道当蓝牙处于开启模式且位置服务处于关闭模式时,有没有办法扫描信标。

我已经使用过这段代码,但是当位置服务关闭时,不会调用此委托方法

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region 

有人可以详细说明如何在没有位置服务的情况下扫描信标吗?

提前致谢。

3 个答案:

答案 0 :(得分:1)

不,我认为您无法在关闭位置服务的情况下使用系统iBeacon支持。 iBeacon支持由Core Location框架提供。

您可以编写低级别的BLE代码来查找来自iBeacons的消息,但我看过那些无法弄清楚如何做的人的帖子。此外,您将失去Core Location提供的优势,例如即使在检测到信标时您的应用未运行,也会收到有关信标的通知。

答案 1 :(得分:1)

启用蓝牙但位置关闭,您只能使用CoreBluetooth API检测蓝牙LE信标设备,而不能使用CoreLocation API。

这有效地阻止您检测iBeacon传输,因为iOS会通过CoreBluetooth阻止其检测。请在此处查看我的博文:http://developer.radiusnetworks.com/2013/10/21/corebluetooth-doesnt-let-you-see-ibeacons.html

但是,您可以使用CoreBluetooth来检测其他信标格式,例如AltBeacon和Eddystone。我有开源代码,展示了如何使用iOS beacon tools

使用上述工具,以下是扫描AltBeacon,Eddystone-UID,Eddystone-URL和Eddystone-EID并记录检测到的标识符的示例:

self.beaconParsers = [[NSMutableArray alloc] init];
RNLBeaconParser *altBeaconParser = [[RNLBeaconParser alloc] init];
[altBeaconParser setBeaconLayout:@"m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25" error: Nil ];
RNLBeaconParser *uidBeaconParser = [[RNLBeaconParser alloc] init];
[uidBeaconParser setBeaconLayout:@"s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19" error: Nil];
RNLBeaconParser *urlBeaconParser = [[RNLBeaconParser alloc] init];
[urlBeaconParser setBeaconLayout:@"s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v" error: Nil];
RNLBeaconParser *eidBeaconParser = [[RNLBeaconParser alloc] init];
[eidBeaconParser setBeaconLayout:@"s:0-1=feaa,m:2-2=30,p:3-3:-41,i:4-11" error: Nil];
self.beaconParsers = @[ altBeaconParser, uidBeaconParser, urlBeaconParser, eidBeaconParser ];
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()
                                                  options:@{ CBCentralManagerOptionRestoreIdentifierKey:
                                                               @"myCentralManagerIdentifier" }];

...

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  if (central.state == CBCentralManagerStatePoweredOn && self.scanning) {
    CBUUID *eddystone16BitUUID = [CBUUID UUIDWithString:@"FEAA"];
    NSLog(@"eddy uuid is %@", [eddystone16BitUUID UUIDString]);
    [self.cbManager scanForPeripheralsWithServices:@[eddystone16BitUUID] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @(YES)}];
    // this scans for BLE peripherals including beacons
    [self.cbManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @(YES)}];

  }
  else {
    if (central.state == CBCentralManagerStateUnknown) {
      NSLog(@"CoreBluetooth state UNKNOWN");
    }
    else if (central.state == CBCentralManagerStateResetting)  {
      NSLog(@"CoreBluetooth state RESETTING");
    }
    else if (central.state == CBCentralManagerStateUnsupported)  {
      NSLog(@"CoreBluetooth state UNSUPPORTED");
    }
    else if (central.state == CBCentralManagerStateUnauthorized)  {
      NSLog(@"CoreBluetooth state UNAUTHORIZED");
    }
    else if (central.state == CBCentralManagerStatePoweredOff)  {
      NSLog(@"CoreBluetooth state POWERED OFF");
    }
  }
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
  NSDictionary *serviceData = advertisementData[@"kCBAdvDataServiceData"];

  RNLBeacon *beacon = Nil;
  NSData *adData = advertisementData[@"kCBAdvDataManufacturerData"];

  for (RNLBeaconParser *beaconParser in self.beaconParsers) {
    if (adData) {
      beacon = [beaconParser fromScanData: adData withRssi: RSSI forDevice: peripheral serviceUuid: Nil];
      beacon.bluetoothIdentifier = [peripheral.identifier UUIDString];
    }
    else if (serviceData != Nil) {
      for (NSObject *key in serviceData.allKeys) {
        NSString *uuidString = [(CBUUID *) key UUIDString];
        NSScanner* scanner = [NSScanner scannerWithString: uuidString];
        unsigned long long uuidLongLong;

        [scanner scanHexLongLong: &uuidLongLong];
        NSNumber *uuidNumber = [NSNumber numberWithLongLong:uuidLongLong];

        NSData *adServiceData = [serviceData objectForKey:key];
        if (adServiceData) {
          beacon = [beaconParser fromScanData: adServiceData withRssi: RSSI forDevice: peripheral serviceUuid: uuidNumber];
        }
      }
    }
    if (beacon != Nil) {
      break;
    }
  }

  if (beacon != Nil) {
    NSString *key = [NSString stringWithFormat:@"%@ %@ %@", beacon.id1, beacon.id2, beacon.id3];
    NSLog(@"Detected beacon: %@", key);
}

答案 2 :(得分:0)

不,没有位置服务就无法使用iBeacon。检查苹果文档中的内容:

  

iBeacon适用于iOS中的位置服务。使用iBeacon,您的iOS   当您接近或离开某个位置时,设备可以提醒应用。在   除了监控您的位置,应用程序还知道您何时关闭   到iBeacon,就像零售商店的收银台一样。代替   使用纬度和经度来确定您的位置,iBeacon使用   您的iOS设备检测到的蓝牙低能量信号。

有关详细信息,请参阅Apple support for iBracon

希望这会有所帮助:)