我正在为iOS编写一个应用程序,它要求应用程序同时通告iOS iBeacon和外围服务。有必要对服务进行广告,而不是简单地在外围设备上发现服务,因为使用案例需要在由于靠近iBeacon而被iOS唤醒(但仍然在后台)之后,中央连接到外围设备(在BLE的说法中)。在中央设备上的后台运行的应用程序只能通过可用的服务来发现外围设备,而不是发现所有的外围设备[];我的代码可以宣传服务或iBeacon,但我还没有想出如何同时做这两件事。有可能iBeacon使用了38字节可用空间中的21字节,并且根本没有足够的空间来通告信标和服务?
这是有效的(信标):
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 ];将两者加在一起,试图同时宣传这两项服务是行不通的。它只通告信标,而不是服务:
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 ];感谢您的关注!
发布于 2013-11-27 06:02:24
我能够通过分别为接收器和信标使用单独的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{
}
}这里唯一的限制是设备无法检测到自己。
发布于 2014-08-16 19:36:12
在我的实践中,iBeacon和BLE服务不能同时发布广告。
如果广告与iBeacon混合,BLE服务不能在前台广告。iBeacon不能在后台发布广告。
iBeacon & BLE服务可以逐个广告,例如iBeacon广告0.5秒,然后BLE服务广告30.0秒。
希望这能有所帮助
https://stackoverflow.com/questions/19351856
复制相似问题