iOS CoreBluetooth / iBeacon:同时发布iBeacon和外围服务

时间:2013-10-14 00:22:32

标签: iphone core-bluetooth bluetooth-lowenergy ibeacon

我正在编写iOS应用程序,要求应用程序同时宣传iOS iBeacon以及广告外围服务。有必要公布该服务,而不是在外设上可以发现,因为用例需要中央(用BLE说法)在被iOS唤醒(但仍在后台)后由于接近iBeacon而连接到外设。在中央运行的后台应用程序只能通过可用服务发现外围设备,而不是发现所有外围设备[];我的代码用于宣传服务或iBeacon,但我还没有弄清楚如何同时做两件事。 iBeacon可能使用了32字节的38字节可用空间,而且根本没有足够的空间来宣传信标和服务?

这有效(信标):

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid 
    major:1 
    minor:1 
    identifier:@"bentboolean"];
NSMutableDictionary *dict = [[self.beaconRegion peripheralDataWithMeasuredPower:nil] mutableCopy];    
[self.peripheralManager startAdvertising:dict ];

这适用于(服务):

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@[serviceUUID] forKey:CBAdvertisementDataServiceUUIDsKey];
[self.peripheralManager startAdvertising:dict ];

将两者放在一起,尝试同时宣传这两种服务是行不通的。它只宣传Beacon,而不是服务:

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid 
    major:1 
    minor:1 
    identifier:@"bentboolean"];
NSMutableDictionary *dict = [[self.beaconRegion peripheralDataWithMeasuredPower:nil] mutableCopy];  
[dict setValue:@[serviceUUID] forKey:CBAdvertisementDataServiceUUIDsKey];  
[self.peripheralManager startAdvertising:dict ];

谢谢你看看!

2 个答案:

答案 0 :(得分:0)

在我的实践中,iBeacon& BLE服务不能同时做广告。

如果广告与iBeacon混合,则BLE服务无法在前台进行广告宣传。 iBeacon不能在后台做广告。

iBeacon& BLE服务可以逐个广告,例如,然后iBeacon宣传0.5秒 BLE服务广告30.0秒。

希望这会有所帮助

答案 1 :(得分:-1)

我能够分别为接收者和信标分别使用单独的CLLocationManager和CLBeaconRegion:

#import "GRBothViewController.h"

@interface GRBothViewController ()

@end

@implementation GRBothViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    self.locationScanner = [[CLLocationManager alloc] init];
    self.locationScanner.delegate = self;

    [self initBeacon];
    [self initRegion];
    [self locationManager:self.locationManager didStartMonitoringForRegion:self.scannerRegion];
}

- (void)initBeacon {
    NSLog(@"Starting beacon");

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString: @"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                                major:1
                                                                minor:1
                                                           identifier:@"com.thisisgrow.Grow"];
    [self transmitBeacon:self];
}

- (IBAction)transmitBeacon:(id)sender {
    self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
                                                                     queue:nil
                                                                   options:nil];
}

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
    if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
        NSLog(@"Powered On");
        [self.peripheralManager startAdvertising:self.beaconPeripheralData];
    } else if (peripheral.state == CBPeripheralManagerStatePoweredOff) {
        NSLog(@"Powered Off");
        [self.peripheralManager stopAdvertising];
    }
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)initRegion {
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
    _scannerRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.thisisgrow.Grow"];
    _scannerRegion.notifyEntryStateOnDisplay = YES;
    [_locationScanner startMonitoringForRegion:self.scannerRegion];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    //SCANNER
    [self.locationScanner startRangingBeaconsInRegion:self.scannerRegion];
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    //SCANNER HAS LEFT THE AREA
    [self.locationScanner stopRangingBeaconsInRegion:self.scannerRegion];
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    NSLog(@"Beacons: %d", [beacons count]);
    if(beacons && [beacons count]>0){
        beacon = [beacons firstObject];
    }
    else{

    }
}

此处唯一的限制是设备无法检测到自身。