Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS CoreBluetooth/iBeacon:同时发布iBeacon和外围服务_Iphone_Core Bluetooth_Bluetooth Lowenergy_Ibeacon - Fatal编程技术网

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

Iphone iOS CoreBluetooth/iBeacon:同时发布iBeacon和外围服务,iphone,core-bluetooth,bluetooth-lowenergy,ibeacon,Iphone,Core Bluetooth,Bluetooth Lowenergy,Ibeacon,我正在为iOS编写一个应用程序,它要求应用程序同时发布iOS iBeacon和外围服务。有必要宣传服务,而不是简单地在外围设备上发现服务,因为用例要求在被iOS唤醒后(但仍在后台)连接到外围设备,因为靠近iBeacon。在Central上后台运行的应用程序只能通过可用服务发现外围设备,而不能发现所有外围设备[];我的代码既可以宣传服务,也可以宣传iBeacon,但我还不知道如何同时做到这两个方面。有可能iBeacon使用了38字节可用空间中的21字节,而没有足够的空间来宣传信标和服务 这个工程(

我正在为iOS编写一个应用程序,它要求应用程序同时发布iOS iBeacon和外围服务。有必要宣传服务,而不是简单地在外围设备上发现服务,因为用例要求在被iOS唤醒后(但仍在后台)连接到外围设备,因为靠近iBeacon。在Central上后台运行的应用程序只能通过可用服务发现外围设备,而不能发现所有外围设备[];我的代码既可以宣传服务,也可以宣传iBeacon,但我还不知道如何同时做到这两个方面。有可能iBeacon使用了38字节可用空间中的21字节,而没有足够的空间来宣传信标和服务

这个工程(灯塔):

本工程(服务):

将两者相加,试图同时为这两项服务做广告是行不通的。它只宣传信标,不宣传服务:

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 ];

谢谢你看

我可以使用单独的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{

    }
}

这里唯一的限制是设备无法检测自身。

在我的实践中,iBeacon&BLE服务不能同时发布

如果播发与iBeacon混合,则BLE服务不能在前台播发。 iBeacon无法在后台播发

iBeacon&BLE服务可以一个接一个地播发,例如iBeacon播发0.5秒,然后 BLE服务广告30.0秒


希望这会有帮助

嗨,你解决过这个问题吗?我不认为这是可能的,因为蓝牙的容量…另一个有趣的观察我看到:即使另一个应用程序广告为iBeacon,你的应用程序无法检测到它。更重要的是,如果存在一个外部iBeacon来宣传相同的标识符,那么您的应用程序仍然无法检测到它。换句话说,公布iBeacon ID集会阻止对同一ID集的任何检测。不过,当将设备用作iBeacon时,如果您想将其用于此目的,自动为设备生成唯一的UUID(如果需要,甚至在启动时)非常简单。对我来说,最大的限制因素是1)用作iBeacon的设备根本不会在后台播放,2)应用程序不能在后台为单个iBeacon设置范围,除非在OnNet和onLeave事件中被唤醒,这些事件只有在进入或离开UUID区域时才会触发。@d2burke您提到,用作iBeacon的设备根本不会在后台广播。你知道即使应用程序使用蓝牙外围设备背景模式,情况是否如此吗?@darrinm-不幸的是,是的。iBeacon框架实际上与核心蓝牙是分离的,其行为方式也不同。我不确定这个回答是如何接近于回答这个问题的。问题是:“我能把自己宣传成一个灯塔和一个可以提供服务的外围设备吗?”。此回复将广告作为信标进行讨论,并尝试检测信标。
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 ];
#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{

    }
}