iOS CoreBluetooth / iBeacon: Advertise an iBeacon and a peripheral service concurrently

后端 未结 2 379
南旧
南旧 2020-12-09 09:58

I\'m writing an application for iOS that requires that the application advertise both an iOS iBeacon as well as advertise peripheral service concurrently. It\'s necessary th

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 10:56

    I was able to get this going with a separate CLLocationManager and CLBeaconRegion for both the reciever and the beacon separately:

    #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{
    
        }
    }
    

    The only limitation here is that the device cannot detect itself.

提交回复
热议问题