0

Is there a way to find out altBeacon's major and minor values? I am using this library (https://github.com/CharruaLab/AltBeacon) for detecting nearby BLE-devices (whether it beacons or not). Beacons are detected, and I know their uuids. Can I extract major and minor values using iOS CoreBluetooth framework?

UPDATE: The code seems fine. Looks like the only way to find out the major or the minor is requesting for beacon's data as it's done in accepted answer below. But the beacons itself is not necessarily will respond to you in such a case.

Denis Kharitonov
  • 199
  • 1
  • 15

1 Answers1

2

For iBeacon Only:


Override CLLocationManager delegate method below, this method will call when you able to detect beacons.

-(void)locationManager:(CLLocationManager*)manager
       didRangeBeacons:(NSArray*)beacons
              inRegion:(CLBeaconRegion*)region {
    // Beacon found!
    
    CLBeacon *foundBeacon = [beacons firstObject];
    
    // You can retrieve the beacon data from its properties
    NSString *uuid = foundBeacon.proximityUUID.UUIDString;
    NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
    NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
}

To detect beacons using Core location framework you need to add below code in viewDidLoad method.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // Initialize location manager and set ourselves as the delegate
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
    // Create a NSUUID with the same UUID as the broadcasting beacon
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"];
    
    // Setup a new region with that UUID and same identifier as the broadcasting beacon
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:@"com.app.identifier"];
    
    // Tell location manager to start monitoring for the beacon region
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
    
    // Check if beacon monitoring is available for this device
    if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
        [alert show]; 
    }
}

For Bluetooth device:


Extend view controller with two delegates CBCentralManagerDelegate and CBPeripheralDelegate

In viewDidLoad create CBCentralManager object.

_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

And override below methods (except scan method):

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }
    // The state must be CBCentralManagerStatePoweredOn...
    // ... so start scanning
    [self scan];
}

- (void) scan {
    
    // UUID for peripheral services
    [self.centralManager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX"]] options:nil];
    NSLog(@"Scanning started");
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    
    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
    
    if (self.discoveredPeripheral != peripheral) {
        
        // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
        self.discoveredPeripheral = peripheral;
        
        // And connect
        NSLog(@"Connecting to peripheral %@", peripheral.UUID);
        [self.centralManager connectPeripheral:peripheral options:nil];
    }
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"Peripheral Connected");
    
    // Stop scanning
    [self.centralManager stopScan];
    NSLog(@"Scanning stopped");
    
    // Make sure we get the discovery callbacks
    peripheral.delegate = self;
    [peripheral discoverServices:nil];
}
Community
  • 1
  • 1
Kampai
  • 22,848
  • 21
  • 95
  • 95
  • this works for iBeacons only, as I understand. 'Cause altBeacons trigger BT-framework delegates. – Denis Kharitonov Feb 18 '15 at 11:54
  • FYI: If altBeacons is an iBeacon than it must support CoreLocation API as per Apple standard and MFI program. Please check altBeacons API and let me know. – Kampai Feb 18 '15 at 11:56
  • Have a look on edit. I have check `altBeacons ` git repo, There is no details about major and minor. It advertise data in bytes, which can be easily get from above method, also you can get services if device can offer by override more delegate method from Core Bluetooth. – Kampai Feb 18 '15 at 12:10